Goodbye Git Master Branch

February 6, 2021

Switching off the git master branch was easier than I expected!

Changes are coming…

I had heard about the controversy about git’s master branch. I didn’t have a strong emotional attachment to the name… so I decided to switch and see what happens.

It seems like main is the consensus for the new branch name:

Let’s switch everything to main.

New Repositories, on GitHub

Since October 2020, GitHub creates new repositories with main as the default branch.

Done ✅

New Repositories, locally

Since git 2.28, there’s a new config setting for the default branch name on newly created repositories:

> git config --global init.defaultBranch main

Done ✅

Existing Repositories…

It took a few tries to find a recipe that I was happy with.

For each repository, pick one clone:

> git branch -m master main
> git push -u origin main
> git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

# remote is github?
# - visit https://github.com/$username/$project/settings/branches
# it's under "settings" >> "branches"
# - rename default branch

# remote is bare repo?
# - cd to its directory
> git symbolic-ref HEAD refs/heads/main

# back to local directory
> git push origin --delete master

and for all your other clones:

> git branch -m master main
> git branch -u origin/main

I audited all my local repositories and had the chance to practice on over 20 projects. ✅

Then, I remembered to repeat on all my computers. ✅

Your own local audit

If you’ve been accumulating clones over time, it’s easy to lose track of ALL the git repositories you have hanging around. This can be a good start:

# from your $HOME
> find . -name 'config' -type f 2>/dev/null | grep '\.git/config'

If you save the output to a file, you can bootstrap the next phase: checking “ownership”

# output from previous command, saved to "all_repos"
> grep github.com:jpalardy $(cat all_repos)

You’ll want to adjust this to your own git remote names, usernames, etc

Consequences

Everything just worked … which wasn’t what I was expecting. I was planning on breaking something… researching and fixing problems over time. 😅

Intellectually, I understood that commits matter, but branches are just convenient names. Of course, branches can have different names locally and remotely … (although I never thought that would be a good idea)

References

Sounds too easy? Not sure? Here are some of the pages that helped me:

Discuss on Twitter