Rename a Git Branch

Renaming a Git branch — particularly the default branch — requires coordinated local and remote changes. The following procedure renames master to trunk (or any other name) across local and remote repositories.

Procedure

git checkout -b trunk master # Create and switch to the trunk branch
git push -u origin trunk     # Push the trunk branch to the remote and track it
git branch -d master         # Delete local master

Update the remote default branch

Before deleting the remote master branch, change the repository’s default branch on the hosting platform:

  • GitHub: Settings → Branches → Default branch → switch to trunk
  • Bitbucket: Repository settings → Main branch → change from master to trunk
  • GitLab: Settings → Repository → Default branch

Clean up the old branch

git push --delete origin master # Delete remote master
git remote prune origin         # Delete the remote tracking branch

Alternative: rename in place

For a purely local rename (no remote yet):

git branch -m old-name new-name

The -m flag is shorthand for --move. To rename the current branch without specifying the old name:

git branch -m new-name

Sources

Related: git-push-pull-different-remotes, git-alternate-ssh-key