How to Rename the master branch to main in Git
A word of warning
Please pay attention that this is mainly for lone wolf developer / sysadmins who do not use Github to upload their code.
If you use Github, this still works, execpt that Github may not allow you to delete your master
branch and extra steps are needed that are not part of this post.
Renaming the local repository
First rename the master
branch in your local repository:
1use $ git branch -m master main
If you want to check:
1$ git status
2On branch main
3Your branch is up to date with 'origin/master'.
4
5nothing to commit, working tree clean
Renaming the remote master branch
Git does not allow to rename remotely a branch. So we will create a new remote branch by pushing the current content of the branch (main
) and then delete the old branch.
1$ git push -u origin main
Let’s delete the old master
remote branch.
1$ git push origin --delete master