More on branch name changes
2020-11-27 08:38 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
You may remember this post about renaming the default branch in Git repositories. Since then I've done some script writing -- they say you don't really understand a process until you can write a program that does it, and this was no exception. (There are lots of exceptions, actually, but that's rather beside the point of this post...)
Anyway, here's what I think is the best way to rename master to main in a clone of a repository where that rename has already been done. (That's a common case anywhere you have multiple developers, each with their own clone, or one developer like me who works on a different laptop depending on the time of day and where the cats are sitting.)
git fetch git branch -m master main git branch -u origin/main main git remote set-head origin main git remote prune origin
The interesting part is why this is the best way I've found of doing it: 1. It works even if master isn't the current branch, or if it's out of date or diverged from upstream. 2. It doesn't print extraneous warnings or fail with an error. Neither of those is a problem if you're doing everything manually, but it can be annoying or fatal in a script. So here it is again, with commentary:
git fetch
-- you have to do this first, or the git
branch -u ...
line will fail because git will think you're setting
upstream to a branch that doesn't exist on the origin.
git branch -m master main
-- note that the renamed branch
will still be tracking master. We fix that with...
git branch -u origin/main main
-- many of the pages I've seen
use git push -u...
, but the push isn't necessary
and has several different ways it can fail, for example if the current
branch isn't main or if it isn't up to date.
git remote set-head origin main
-- This sets main as the
default branch, so things like git push
will work
without naming the branch. You can use -a
for "automatic"
instead of the branch name, but why make git do extra work? Many of the
posts I've seen use the following low-level command, which works but isn't
very clear and relies on implementation details you shouldn't have to
bother with:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
git remote prune origin
-- I've seen people suggesting
git fetch --prune
, but we already did the fetch way
back in step 1. Alternatively, we could use --prune
on that
first fetch, but then git will complain about master tracking a branch
that doesn't exist. It still works, but it's annoying in a script.
Just as an aside because I think it's amusing: my former employer (a large online retailer) used and probably still uses "mainline" for the default branch, and I've seen people suggesting as an alternative to "main". It is, if anything, more jarring than "master" for someone who has previously encountered "mainlining" only in the context of self-administered street drugs.
Another fine post from
The Computer Curmudgeon (also at
computer-curmudgeon.com).
Donation buttons in profile.