Renaming Branches
Branch names are just labels — you can change them whenever you want. Renaming is a tiny, fast operation locally. The only complication is when the branch has already been pushed to a remote, in which case there are a couple of extra steps so collaborators can adjust.
Renaming the current branch
The simple form
# You are on the branch you want to rename git branch -m new-name # Now the current branch is called new-name
Renaming a different branch
Two-argument form
git branch -m old-name new-name # Renames 'old-name' to 'new-name' — you do not need to be on it
Force rename (overwriting)
-M overrides safety checks
# If a branch named 'new-name' already exists, -m refuses git branch -m old-name new-name # error: A branch named 'new-name' already exists. # -M (capital) force-overwrites git branch -M old-name new-name
Renaming an already-pushed branch (the proper way)
Three steps: rename local → push new → delete old remote
# 1. Rename locally git branch -m old-name new-name # 2. Push the renamed branch and set upstream git push origin -u new-name # 3. Delete the old name on the remote git push origin --delete old-name # (Optional) 4. Tell collaborators to do the same locally: # git fetch # git branch -m old-name new-name # git branch --unset-upstream # git branch -u origin/new-name
Renaming master → main
The most common rename in modern Git: changing the default branch from master to main.
Step-by-step
# 1. Locally git switch master git branch -m master main # 2. Push the new name git push -u origin main # 3. Update the default branch on your hosting provider: # GitHub: Settings → Branches → Change default branch # GitLab: Settings → Repository → Default Branch # Bitbucket: Repository settings → Branching model # 4. Delete the old branch on the remote git push origin --delete master # 5. (Per teammate) pull and rename locally: git fetch --all git branch -m master main git branch --unset-upstream git branch -u origin/main git remote set-head origin -a
Setting a new default for new repos
Avoid this in the future
git config --global init.defaultBranch main
Updating tracking when remote was renamed by someone else
If a teammate renamed and you fetched
git fetch --all --prune git branch -m old-name new-name git branch -u origin/new-name
Symbolic refs and pull-request links
GitHub and GitLab automatically redirect PR URLs after a rename — but only if the old branch was deleted via the platform. The manual
git push --deletedoes not always trigger that.CI configurations that hardcode the branch name (
build only on master) will need to be updated.Branch protection rules and required-checks settings often need to be re-applied to the new name.
Listing renamed branches in the reflog
git reflog new-name # Shows the history of HEAD movements on this branch — including the rename event