Set default git branch to main

Posted on 2020-06-25 by

For a while, we've all been seeing the "switch git default branch from master to main" posts, the earliest I recall having been written by Scott Hanselman. I've been postponing the change for a bit, but it was the post by Kristófer Reykjalín that gave the required motivation to go out and just do it.

For new repositories, Gitea already has the option to set the default branch name.

For exiting repositories, the commands provided by Scott work perfectly:

git checkout master; git branch -m master main; git push -u origin main

Yes, a one-liner :) If you like to take things more slowly, here it goes:

git checkout master         # switch to master branch
git branch -m master main   # move existing master branch to main (keeping history)
git push -u origin main     # push the main branch to the server

If you use Gitea, you should now go the repository's Settings > Branches and set the main branch as the default branch. Once done, you can now safely delete the obsolete master branch with the command below.

git push --delete origin master     # delete the master from the server

As for my website, here's the commit that completed the transition, making sure my CI/CD solution knows what to listen to.


Update 1

Thanks to Charles Pence for reminding me to add the git push --delete origin master line.


Update 2

Thanks to Charles Pence for reminding me that a Gitea branch cannot be deleted as long as it's the default.

Webmentions

This post has not been mentioned yet.