Rename a branch locally and on the remote
How do I rename a Git branch?
Short answer
git branch -m <new-name> renames the branch you're on. Renaming on the server takes two more steps, because Git has no rename operation for remote branches — you push the new name and delete the old one.
git branch -m new-name
git push -u origin new-name
git push origin --delete old-name
Rename locally, publish the new name, remove the old one
Does this match your situation?
- You made a typo in the branch name.
- The branch name doesn't match your team's convention or ticket number.
- You want to rename master to main.
Step-by-step fix
Rename the local branch
With -m you rename the branch you're currently on. Give two names to rename a different branch without switching to it.
step 1git branch -m new-name # rename another branch from where you are: git branch -m old-name new-namePush the new name and set it as upstream
The server still has the old branch; this creates the new one alongside it and points your local branch at it.
step 2git push -u origin new-nameDelete the old branch on the server
Only once the new name is pushed and anything depending on it (open pull requests, CI) has been repointed.
step 3git push origin --delete old-nameIf the old branch is protected, or a PR still targets it, change that in your host's settings first.
Tell everyone else to update
Other clones still track the old name and will keep showing it until they prune. This is the step that causes confusion if skipped.
step 4# each teammate runs: git fetch --prune git branch -m old-name new-name git branch -u origin/new-name new-nameRenaming master to main
Same mechanics, plus updating the default branch in your hosting platform's settings so new clones check out the right thing.
step 5git branch -m master main git push -u origin main # change the default branch in the repo settings on GitHub/GitLab FIRST git push origin --delete master
Why this works
A branch is a file under .git/refs/heads containing one commit hash, so renaming locally is genuinely just renaming that file — no commits move and nothing is rewritten. Remote branches have no rename operation in the Git protocol at all; you can only create and delete refs. That's why the remote 'rename' is really a create-then-delete, and why everyone else's clone keeps the stale name until they fetch with pruning.
How to stop it happening again
- Agree a branch naming convention (feature/, fix/, chore/) and stick to it.
- Set init.defaultBranch main globally so new repositories start correctly.
- Create branches from the ticket ID to avoid inventing names under pressure.
Commands used in this guide
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
How do I rename a Git branch?
git branch -m new-name renames the branch you're on. To rename one you're not on, pass both names: git branch -m old-name new-name. Neither touches the remote.
How do I rename a branch on GitHub?
Push the new name with git push -u origin new-name, then delete the old one with git push origin --delete old-name. GitHub also has a rename button in the branch settings, which additionally retargets open pull requests for you.
Will renaming a branch lose my commits?
No. A branch is just a pointer to a commit; renaming changes the label, not the history. Every commit stays exactly where it is.
Related rescue guides
How do I delete a remote branch in Git?
git push origin --delete <branch> removes it from the server. Deleting it locally with git branch -d doesn't touch the remote at all, and other people keep seeing stale copies until they run git fetch --prune.
Read the fix →Usually recoverableI deleted a Git branch — how do I get it back?
Deleting a branch removes a pointer, not the commits. Find the branch's last commit hash in the reflog and recreate the branch at that hash with git branch <name> <hash>. If the branch was pushed, the copy on the server may still exist and can simply be fetched back.
Read the fix →Always fixableI committed to main instead of my feature branch — how do I move the commits?
Create a branch at your current position to keep the commits, then reset the branch you polluted back to where it should be. Nothing is lost because the new branch holds the commits the whole time.
Read the fix →Always fixableHow do I change the remote URL of a Git repository?
git remote set-url origin <new-url> repoints an existing remote. It's a purely local change — nothing on either server is touched — so it's the safe way to switch between HTTPS and SSH, move to a new host, or fix a URL after renaming the repository.
Read the fix →