Git aliases to improve working on projects which have different names for their default branch

Now that more git repositories are changing the default branch name to something other than master, I’ve found I struggle to remember what the default branch name is for different projects.

I have added two git aliases which solve this issue for me and I thought I’d share them here in case they are useful to someone else:

  • The first one is called default-branch and it will print out the name of the default branch for a git repository
  • The second is called checkout-default and it will do a git checkout to the default branch for a git repository

You can install these aliases by running the below commands:

git config --global alias.default-branch '!git remote show origin | awk "/HEAD branch/ {print \$NF}"'
git config --global alias.checkout-default '!git checkout $(git default-branch)'

Now when you want to be on the default branch, instead of having to remember, or find out the default branch manually, you can instead do git checkout-default.