Last July, some of the git hosting platforms (GitHub, GitLab, and Bitbucket) changed the default branch name to
master). However, Git did not! 🤦
It’s disappointing that everyone didn’t act in unison to make the same change, because it has created some work for users. The focus of this post is on how this impacts maintenance of public repositories, and nothing else. I hate meddling with configuration, so that is where my disappointment lies!
I managed to avoid this altogether until now. It was only creating a new repo on GitHub this week that it surfaced again. So, it’s time to set things up to avoid this in future.
How can I fix this for new repos?
You can change the default name in Git, or change the default name on your git hosting provider.
It’s probably easier to do it in Git, especially if you use more than one provider.
Change default branch name in Git
Git 2.28 (released in July 2020) introduced
init.defaultBranch configratuion option. Check you have the 2.28 or later installed to avail of this.
If you don’t, download and install the latest.
Now, you can update the default branch name.
git config --global init.defaultBranch main
Change default branch name in GitHub/GitLab
- See GitHub Docs - Managing the default branch name for your repositories.
- See GitLab Docs - Custom initial branch name.
And what about existing repos?
For existing repos, you can still push and pull without needing to rename the default branch. So, option 1 is to do nothing!
Option 2 is to rename. You need to consider what other people are doing before you go ahead, it will annoy people if they have existing PRs and you don’t let them know!
Shortest way (?)
The shortest set of steps is probably:
Disable Default Branch and Protected Branches settings if applicable.
Go to the master branch.
Fetch latest (if in doubt).
Rename the branch to
main. This preserves history.
Push changes and update upstream remote link (
git push -u origin main).
git checkout master
git branch -m master main
git push -u origin main
Go to the Settings on GitHub (Settings > Branches > Default Branch)or GitLab (Settings > Repository > Default Branch) and change the default branch to
git push origin --delete master
Don’t forget to update related CI/CD configurations
If you do change exisiting repos, do not forget your build chain. You will need to update their configuration to be in step with your repo. If they depend on an “origin/master” branch, they will complain eventually, to someone!
If you deploy to Netlify using a git repo, you’ll need to tell Netlify you’ve updated the default branch (what they call the production branch).
- Log in and find the site
- Go to Deploys
- Click Deploy Settings
- Under Deploy contexts click Edit settings
- Change the name of the production branch and click Save
Note: There may still be a “branch deploy” for master after you click save. You can remove this by just editing the settings one more time and removing master from the “Let me add individual branches” section.
GitHub Actions & GitLab CI/CD
A simple find-and-replace of “master” in your configuration files should be sufficient.
I chose to update my Git configuration. Going forward, I will use the default of
main for new projects. I will leave my existing projects as they are.