Dealing with changes to git default branches

As you may have heard by now, the default git branch name is changing from master to main in most git software (GitHub, BitBucket, GitLab, etc.) to drive out some divisive terminology. A lot of tutorials I see include scripts and aliases that depend on the default branch name being master, and those tools may be broken now, leaving you and your users with some issues after the migration is completed server-side.

The good news is that the default branch isn’t just a “thing you know”, it can be extracted dynamically from a git repo. Doing so is a very simple piece of code:

git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'

You can integrate this into any other aliases or scripts you have. For example, in my .bashrc, I’ve replaced these branch-specific aliases:

alias updatemaster='git checkout master && git fetch origin --prune && git fetch origin --tags && git pull origin master'
alias updateprod='git checkout production && git fetch origin --prune && git fetch origin --tags && git pull origin production'

with this single non-specific alias:

alias updateorigin='git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') && git fetch origin --prune && git fetch origin --tags && git pull origin $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')'

And for my VS Code setup on Windows, where I use a PowerShell terminal instead of WSL/bash, my .gitconfig contains an alias for git up:

[alias]
up = !"git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'); git fetch --prune; git pull"

Regardless of whatever system you use, now is a good time to start replacing any hardcoded instances of master and replace it with a more dynamic reference so that changes to the default branch are transparent to you. Share this information with your coworkers and update your training and new hire documents and your whole company can make the transition smooth, too.

2 thoughts on “Dealing with changes to git default branches

  1. Hi! I was looking at your post about making a symlink to your control-repo in the .fixture.yml file. But do you have any repo that shows the entierity of acceptance testing a control repo?

Leave a Reply to Kriss Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s