Set Git default branch

We will set git default branch and change master to main in this article.

There was a discussion of master slave some time ago, and git by default used master branch. But things are changed and git now suggest you to set master to other branch. Git also allows you to set this to another branch when you create a new repository. If you create a repository next time or initiate git init in a directory, default branch setting will take effect.

Fire up a terminal/console window and type/paste the following command to set git default branch, you can change the branch name to any name of your choice. I like to set it on global level.

git config --global init.defaultBranch main

Now if you have a repository and have master as your default branch, let’s change it to main. Change path to your repository and type:

git checkout master
git branch -m master main

Now push this new branch to the remote repository if there is one.

git push origin main

If you using GitHub, GitLab or Gitea* you can now update the branches like protect main branch, update any CI files, make main the default branch and then delete master branch.

You can now safely update the upstream branch on your local.

git branch --unset-upstream
git branch -u origin/main

*Bonus: if you are using Gitea or you can install an Android app GitNex from play store or f-droid. Disclaimer, I am the core author.

Leave a Reply

Your email address will not be published.