Change Git Default Branch Name

All git repositories used to use the master as the branch name, but it was changed to main since October, 2020. Here are steps to change default branch name for existing projects.

1. Change the branch name locally

git branch -m master main

-m option is used to rename the branch name with all of the commit history transfer to new branch.


2. Push new branch to remote upstream

git push -u origin main

-u option is used to set the upstream


3. Change HEAD pointer to new branch

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

You can use git branch -a to check which branch HEAD points to.


4. Change default branch in the repo host

In github, gitlab or bitbucket, go to settings -> branches and change the default branch name from 'master' to 'main'.

git rename branch

5. Delete old upstream branch

git push origin --delete master