Easily rename your Git default branch from master to main
The Internet Engineering Task Force (IETF) points out that "Master-slave is an oppressive metaphor that will and should never become fully detached from history" as well as "In addition to being inappropriate and arcane, the master-slave metaphor is both technically and historically inaccurate." There's lots of more accurate options depending on context and it costs me nothing to change my vocabulary, especially if it is one less little speed bump to getting a new person excited about tech.
You might say, "I'm all for not using
master in master-slave technical relationships, but this is clearly an instance of master-copy, not master-slave."
UPDATE: There is Good analysis of the whole main branch convo in the Git Rev News: Edition 65. Git likely uses master in the context of "master copy" or "master recording."
So we see that while the word master doesn't always connote slave, for many, it's evocative via basic word-association and they just don't want to look at the word on their prompt all day. Choice is cool.
I have had dozens of git repositories that have 'master' as the main branch. Changing that would be a hassle right?
Let's see. I'll just "git branch -m master main" and then push it back! Remember that -m is --move so your history isn't changed! Even better I can "
git push -u origin main" to set the upstream at the same time.
> git branch -m master main
> git push -u origin main
Total 0 (delta 0), reused 0 (delta 0)
remote: Create a pull request for 'main' on GitHub by visiting:
* [new branch] HEAD -> main
That was easy.
NOTE: Changing the default branch to "main" also has the benefit of starting with "ma" so that autocomplete <TAB> muscle memory still works. Another great option for your main github branch is "latest." The goal is to just be unambiguous.
Now I just need to change my default branch in my GitHub settings for my repository.
I can also update the tracking branch manually as seen here, but if you use
git push -u origin main it'll do both.
git branch -u origin/main main
The last thing to think about is if you have a CI/CD, GitHub Action, Azure DevOps pipeline or some other build system that pulls a specific branch. You'll just change that to main. However, usually unless your CI explicitly calls for a branch by name, changing master to main will "just work!"
NOTE: For more complex repos also check your protected branch rules.
This is because -m is --move and all your reflog is unchanged!
TL;DR in conclusion:
git branch -m master main
git push -u origin main
Updating local clones
If someone has a local clone, then can update their locals like this:
$ git checkout master
$ git branch -m master main
$ git fetch
$ git branch --unset-upstream
$ git branch -u origin/main
$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
From the tweet above (Thanks Brad from XUnit.net!), these steps
- Go to the master branch
- Rename master to main locally
- Get the latest commits from the server
- Remove the link to origin/master
- Add a link to origin/main
- Update the default branch to be origin/main
You can add an alias "
git new" that will default to whatever starting branch you like. (NOTE: This is no longer needed, set below)
git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/main'
UPDATE! As of Git 2.28 you don't need an alias as above, as there is a new config option called init.DefaultBranch. Just set it and forget it.
git config --global init.defaultBranch main
Hope this helps! Other good names are latest, trunk, and stable!
Sponsor: Have you tried developing in Rider yet? This fast and feature-rich cross-platform IDE improves your code for .NET, ASP.NET, .NET Core, Xamarin, and Unity applications on Windows, Mac, and Linux.