Howto rename git master branch to main

Sun, 25 Oct. 2020     Thomas Bendler     ~ 3 min to read

Recent discussions around, for example, the black life matters movement have brought up the question if there are terms we use in IT are historically burdened. Although the association for me as a non-native speaker might be different, I fully support the idea to use terms without any historical burden. One term used in the version control system git is master for the leading main branch. This howto shows how you rename the master branch to main.

It all starts with a set of commands to ensure you are working on the right branch and all maybe open changes are synchronized.

> git checkout master
> git fetch --all -p
> git pull
> git push

Once this is done, you need to create a new local branch called main:

> git branch -m master main
> git status

The last command should reply that you are working on the main branch, that the main branch is in sync with “origin/master” and that there is nothing to commit in the current working directory. The last step in the preparation phase is now to push the new branch name to the remote repository:

> git push -u origin main

At the remote location, you need to set the default branch to main because otherwise, you can’t delete the master branch. Depending on the question where you host your source code, the procedure to set the default branch might differ. I use Github for my source code so I will show the procedure for Github. This is for Github quite easy. At the Github web interface, you have to select the repository your changing the branch name. In the repository settings, you need to select the branch list item. There you need to switch the default branch:

Set default branch to main
Set default branch to main

As this has some implications for the checked-out repositories (they need to switch to main as well), you have to confirm this:

Confirm new default branch
Confirm new default branch

Last but not least, you need to do the post-processing/ clean up work, which could be executed in the shell. The next two commands will first delete the old master branch and then fix the origin assignment:

> git push origin --delete master
> git remote set-head origin --auto

Share on: