A ‘git up’ alias that works for any default branch

Traditionally, git’s default branch was called master - but this isn’t always true, it’s just a default - and one that’s currently undergoing change. GitHub & GitLab have changed their defaults to main and the git project itself is in the process of doing the same.

I have a longstanding git alias - git up - that I use to pull my local checkout up to date with the remote. It’s a shorthand for the following:

  • Change branch to the default branch
  • Fetch all changes from the remote, removing any local branches that have been deleted on the remote
  • Pull any changes from the default remote branch

This used to hardcode master as the default branch, but this was getting less and less reliable, so I figured out a better version that checks the repo’s default branch and uses that:

Add these lines to your ~/.gitconfig file
  head-branch = !"git remote show origin | grep 'HEAD branch' | cut -d' ' -f5"
  up = !"git switch $(git head-branch) && git fetch --all --prune --progress && git pull"


Related Posts