GitRenaming Branches

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

Bash
# 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

Bash
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

Bash
# 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
Warning
`-M` (force) silently overwrites the existing branch. Make sure you do not lose work — `git log new-name` first to see what you are about to clobber.
Renaming an already-pushed branch (the proper way)

Three steps: rename local → push new → delete old remote

Bash
# 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

Bash
# 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

Bash
git config --global init.defaultBranch main
Updating tracking when remote was renamed by someone else

If a teammate renamed and you fetched

Bash
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 --delete does 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

Bash
git reflog new-name
# Shows the history of HEAD movements on this branch — including the rename event
Renames don’t lose commits
A rename only moves a pointer. All commits, the history, and the reflog stay intact under the new name. If something feels broken after a rename, it is almost always a tracking issue, not a data-loss issue.
Tip
Keep branch renames rare. They confuse open PRs, CI configs, and teammates. When you must rename, do all the steps in one sitting and announce it on your team channel.