Rename master Branch
Overview
This describes how to rename the master branch in Git.
Requirements
- An existing master branch
Steps
In this example, we will rename “master” to “main”.
Rename the local master branch
$ git branch -m master main
Check the change
$ git status On branch main Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
Rename the remote master branch
$ git push -u origin main
Remove the remote master branch
$ git push origin --delete master
- If you encounter a similar error message:
To https://github.com/example/example.git ! [remote rejected] master (refusing to delete the current branch: refs/heads/master) error: failed to push some refs to 'https://example@github.com/example/example.git'
- Go to the management page for the remote branch and unset it as the default branch
- Try this step again
- If you encounter a similar error message:
Update any other remaining local repositories (e.g., a local master branch on another computer)
Switch to the master branch
$ git checkout master
Rename the local master branch
$ git branch -m master main
Grab the latest changes from the remote branch into the local branch
$ git fetch
Remove the remote branch connection on the local branch
$ git branch --unset-upstream
Setup a new remote branch connection on the local branch
$ git branch -u origin/main