Last July, some of the git hosting platforms (GitHub, GitLab, and Bitbucket) changed the default branch name to main
(from 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 for me. 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.
git --version
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 probably annoy people if they have existing PRs/clones 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 fetch 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
main
.-
Delete remote
master
branch.
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!
Netlify
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.
Conclusion
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.
You can subscribe to my RSS feed to get my latest posts.
Top comments (6)
No, the plattforms that host git have created work for users.
The change to a different branchname was discussed in the git community on kernel.org and ultimately the majority decided to leave it up to the users via the init.defaultBranch configuration.
That is what huge projects like git do, they vote and implement what the majority decides it wants.
So they actually did "act in unison", just how you wanted them to.
Those that did not participate in that well-established voting-process and "did not act in unison" with the rest of the community, those were the hosting platforms you mentioned.
I hope you don't mind the feedback, but this post would have been much better without the political blame-game in the first paragraph.
It is a sensitive topic and just sparks unneccessary flamewars. Which happened dozens of times on DEV already.
Hi Raphael
The focus in this post is on the pragmatic side of maintaining public repos. What I said is intended to be neutral and is merely discussing the impact and solution.
The quotation you included, 'they' refers to everyone (git and git host providers). I have updated it to 'everyone' to be clear.
People should be free to express their opinion on a topic sensitive or otherwise. It just needs to be respectful. I have no issue with your comment, but you misconstrued what I said. I have no interest in discussing the why or who in this scenario. It needed to be pointed out that there is a divergence between the parties involved. All good 😀👍
Fair enough, english grammar is weird that way, it sounded like it was focused on the Git core. Sorry about that and thank you for the correction! Now it is absolutely clear what you meant and what inspired this post.
Neither do I, so it's all good :)
If I had a seal of approval, I'd give it now!
Thank you for the work you put in this post, it's very appreciated.
Lol thanks Raphael. Ya, Its just to reduce the config pain for others. I always appreciate when someone has solved something and shares a short answer to overcome it quickly.
I wonder how long it'll be before "main" is considered offensive.
I wonder how long until they find out about master thesis at universities or white- and blacklists.