Git aliases supporting main and master How to make your aliases agnostic to the default branch
More and more projects are moving or have moved from a default master
branch to one named main
. I deal with repos that use both. This means Git aliases that assume master
or main
break depending on the repo. With the help of a certain Git config these aliases can be made agnostic to the default branch.
I use Git aliases quite extensively. Some of the aliases involve the default branch. The one I use the most is git com
which was defined as follows:
[alias]
com = switch master
(com
used to denote CheckOut Master
before git switch
existed.)
Once I’ve started working on repos that use main
as the default branch, git com
no longer worked:
$ git com
fatal: invalid reference: master
Git 2.28 introduced a new config called init.defaultBranch
alongside a new warning when initializing a new repo:
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /Users/phil/foo/.git/
By now you might have come across this warning and set init.defaultBranch
globally to whichever value you prefer, e.g. main
:
git config --global init.defaultBranch main
We can now change the alias to a shell script alias to (ab)use this value:
[alias]
com = !git switch "$(git config init.defaultBranch)"
This will read out the config from the repo and fall back to the global value if none is set. Because the repo doesn’t define init.defaultBranch
, it’ll return the global value main
.
Once we find ourselves in a new repo that doesn’t have a main
branch and instead uses a master
branch, the command will fail:
$ git com
fatal: invalid reference: main
That makes sense, the global config is set to main
, but the repo uses master
as the default branch1. All we need to do now is to set the config init.defaultBranch
to master
for the current repo, i.e. without the --global
flag:
git config init.defaultBranch master
Repeating the command now succeeds:
$ git com
Switched to branch 'master'
This approach has worked well for me. When working on a new repo that deviates from my preferred initial branch, as soon as I hit the first error related to the default branch, I simply run one command that is already in my shell history and all my aliases work again.
If you liked this post, you might also like my other post Configuring Git for work.
-
Git doesn’t really have a concept of a default branch except in the exact moment of initializing a new repo where the global config
init.defaultBranch
is respected.main
ormaster
aren’t in any way special; they’re just branches like any other. ↩