Jacob Tomlinson
Home Blog Talks Newsletter About

How to check out the default git branch

2 minute read #git, #open-source, #code

Many open source projects are taking steps to update terminology to be more inclusive. The largest of these changes has been renaming the “trunk” branch of git repositories from master to main.

This is great and I fully support the transition, however this has resulted in some minor annoyances when working on various projects. I use git aliases to checkout out the trunk branch (gcm) and to also pull from upstream repositories (glum) as I generally work on forks. I’m now in a position where some projects still use master as their default branch, some use main, some repos like documentation or websites may use gh-pages, and some use other conventions all together.

To resolve this I’m updated my gcm and glum aliases to use a new gdb alias which queries the upstream repository for it’s default branch name. As my workflow uses forks 99% of the time I have defaulted the remote to upstream but if you use another workflow you may wish to change this to origin or something else.

function gdb () {
	git fetch $REMOTE
	git remote show $REMOTE | grep "HEAD branch" | sed 's/.*: //'

unalias gcm
function gcm () {
       git checkout $(gdb $1)

unalias glum
function glum () {
	git pull upstream $(gdb upstream)

These functions replace my existing aliases provided by oh-my-zsh.


gcm checks out the default branch name from the upstream repository, but also takes an argument to pass in an alternative remote name.

$ gcm  # Checks out the default branch name from the 'upstream' remote
$ gcm origin # Checks out the default branch name from the 'origin' remote


glum pulls the default branch from the upstream remote. As the u in glum stands for upstream this one does not take an argument.

$ glum  # Pull the default branch name from the upstream remote

Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!