Changing the default git branch

There's currently a lot of discussion around the use of language in tech, and particularly so given the recent announcement from GitHub that they are renaming the "master" branch. While the etymologies of terms like "master", "blacklist", and "whitelist" are unrelated to race, we can't escape the racial connotations they possess. For some people, these words are a constant reminder of the racial injustices that they face, and that their ancestors faced under slavery. We can't eradicate racism overnight, but we can make the world a little better one step at a time.

Where suitable alternatives exist, why shouldn't we adopt them? Should developers really be that averse to change? It's not exactly hard! Your "master" branch can become your "main" branch, a "blacklist" can become an "exclude list", and a "whitelist" can become an "include list".

So how do you change the default branch name for git?

Updating existing repos

For existing repos, you can rename the main branch by running the commands below.

# Move the history from the master branch to the main branch
git branch -m master main

# Push the new main branch to the remote
git push -u origin main

If you want to then completely remove the "master" branch from the remote, you first need to change the default branch; on GitHub this can be done at<username>/<repo>/settings/branches. Then you can run git push --delete origin master in your repo and it will remove "master" from your remote repo.

Making the change for future repos

The "master" branch is hardcoded into git, and there's currently no simple configuration option, but we can get around this by creating a template repo that has the branch name we want. Now, whenever we initiliase a new repo, git will use this template to create it with a "main" branch instead of a "master".

# Create a directory to store the template
mkdir -p ~/.config/git/

# Copy the default template
cp -r /usr/share/git-core/templates ~/.config/git/template

# Update the HEAD to use "main"
echo "ref: refs/heads/main" > ~/.config/git/template/HEAD

# Set the template directory for git
git config --global init.templateDir ~/.config/git/template