Menu
Coddy logo textTech

Git: Rename a Branch

Last updated

You rename a branch with git branch -m (the -m is for "move"). With one argument it renames the branch you're on; with two it renames any branch by name. Renaming is purely local - if the branch was already pushed, you also delete the old name on the remote and push the new one.

Try these in the terminal playground - a real shell in your browser, nothing to install.

git branch -m moves the branch name: same commits, new label.

Syntax

CommandWhat it does
git branch -m new-nameRename the branch you're currently on
git branch -m old-name new-nameRename another branch by name
git branch -M new-nameForce-rename (overwrite an existing name)

Renaming a branch that's on the remote

Renaming is local, so a pushed branch needs three extra steps.

StepCommandResult
1git branch -m old newRename the branch locally
2git push origin --delete oldDelete the old name on the remote
3git push -u origin newPush the new name and set upstream

Git rename branch FAQ

How do I rename the branch I'm currently on?
Run git branch -m new-name with just the new name. Git renames the current branch in place - you stay on it, and your commits and history are untouched.
How do I rename a branch without switching to it?
Pass both names: git branch -m old-name new-name. This renames old-name to new-name even if you're currently on a different branch.
How do I rename a branch that's already on GitHub?
Rename it locally with git branch -m old new, delete the old name on the remote with git push origin --delete old, then push the new name and set its upstream with git push -u origin new. Anyone else who has the old branch will need to update their local copy.
What's the difference between -m and -M?
-m (move) renames the branch but refuses if a branch with the target name already exists, so you can't clobber another branch by accident. -M forces the rename, overwriting an existing branch of that name. Use -m unless you specifically intend to overwrite.
Can I practice this online?
Yes. Open the terminal playground to run these commands in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers branching end to end.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED