How to Rename a Git Branch from Master to Main

Many people now prefer to name their primary Git repository branch Main, rather than Master, but think it might be troublesome to change existing repositories. However, it’s really quite simple.

Step 1: Pull the repo and switch to the Master branch
$ git clone <your_repo>.git
$ cd <your_repo>
$ git checkout master
Step 2: Move the Master branch to become Main branch
$ git branch -m master main
Step 3: Push the new Main branch to the origin (e.g. GitHub)
$ git push -u origin main
Step 4 (optional): Delete the old Master branch

WARNING: This will delete the Master branch from the origin

To delete the old Master branch, you first need to go to the Git repository page itself and (on GitHub, for example) click on the branch indicator which shows the number of branches in the repo, or on the branch drop-down menu to select “view all branches”. You can also select settings->branches. You then select the new Main branch and set it as the repo’s default. Once you have made this change, you can either delete the old Master branch in the Web UI, or using the following command line:

$ git push origin --delete master

You have now effectively renamed your repo’s Master branch to Main by pulling the remote repository, moving the Master branch to become Main, and pushing back to the origin. If you wish to, you can then make Main the new default branch, and delete the old Master branch.




Git, Open Source