"From 'master' to 'main': The easy guide to renaming your Git default branch for a more inclusive codebase"
I started heavy development on Firecode.io this week to get it out of beta - and one of the first changes I had to make was renaming my primary branch to "main". In 2020, Github announced that it would be dropping the use of the term "master" for the default branch name on its platform. This decision was made in response to calls for more inclusive language in tech, as "master" has historical connotations of slavery and domination. The change has been supported by many in the tech industry who recognize the importance of taking small but significant steps towards creating more inclusive spaces for everyone.
Since Github is one of the most popular code hosting platforms, this change has had a ripple effect across the tech industry. Many companies and organizations have followed suit and made the switch from "master" to "main" for their default branch names. This has involved going through a series of steps to rename the branch, update links, and make sure that the change is reflected in all relevant places.
Making this change is actually quite simple and should only take a couple of minutes:
The first step is to create a new branch called main
from the existing master
branch.
git branch -m master main
This command renames the master
branch to main
.
Next, you need to push the new main
branch to the remote repository:
git push -u origin main
This command pushes the new main
branch to the remote repository and sets up tracking so that future git push
and git pull
commands work as expected.
The next step is to point the HEAD to the main
branch. You can do this using the following command:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
This command is used to update the HEAD reference in the Git repository to point to the new default branch "main" after renaming the "master" branch. In Git, the HEAD reference points to the current branch or commit that is being worked on. By default, the HEAD reference points to the current branch, which is usually the default branch. In this case, since we renamed the default branch from "master" to "main", we need to update the HEAD reference to point to the new default branch "main". The first argument is refs/remotes/origin/HEAD
, which is the remote tracking branch reference that corresponds to the default branch on the remote repository. The second argument is refs/remotes/origin/main
, which is the new default branch name we want to set the reference to.
Now that you've created and pushed the new main
branch, you need to change the default branch on the Github (or BitBucket or wherever you host your repo). Depending on where you host your repo, this should be a simple change from the web UI.
Finally, you can delete the old master
branch:
git push origin --delete master
And that's it! You've successfully renamed the default branch of your Git repository from master
to main
.
It's worth noting that this change may affect some build systems or scripts that rely on the master
branch name, so you may need to update them accordingly. Additionally, if you're using other Git-based tools, you may need to update them to work with the new main
branch. In my case, I had to rename all my terminal aliases and update multiple cloud build scripts to pull the right code.
The shift towards more inclusive language in tech is an important one, and every small step taken towards creating more welcoming and equitable spaces is a step in the right direction.