Rename master branch for both local and remote Git repositories

The closest thing to renaming is deleting and then recreating on the remote. For example:

git branch -m master master-old
git push remote :master         # Delete master
git push remote master-old      # Create master-old on remote

git checkout -b master some-ref # Create a new local master
git push remote master          # Create master on remote

However, this has a lot of caveats. First, no existing checkouts will know about the rename – Git does not attempt to track branch renames. If the new master doesn’t exist yet, git pull will error out. If the new master has been created. the pull will attempt to merge master and master-old. So it’s generally a bad idea unless you have the cooperation of everyone who has checked out the repository previously.

Note: Newer versions of Git will not allow you to delete the master branch remotely by default. You can override this by setting the receive.denyDeleteCurrent configuration value to warn or ignore on the remote repository. Otherwise, if you’re ready to create a new master right away, skip the git push remote :master step, and pass --force to the git push remote master step. Note that if you’re not able to change the remote’s configuration, you won’t be able to completely delete the master branch!

This caveat only applies to the current branch (usually the master branch); any other branch can be deleted and recreated as above.

Leave a Comment