Rename the current branch
Medium risk
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Renames the branch you are on, both locally and on the remote: the new name is pushed and the old remote branch is removed.
gitbranchrename
Parameters
New name for the current branch.
Current name of the branch (used to clean up the remote).
Commands
Confirm which branch is being renamed
git branch --show-currentRename the local branch
git branch -m <new_name>Publish the branch under its new name and track it
git push -u origin <new_name>Remove the old branch name from the remote
git push origin --delete <old_name>Verification
git branch --show-currentPrints the new branch name; git ls-remote shows only the new name on the remote.
Undo
Rename the local branch back
git branch -m <old_name>Re-publish the original branch name
git push -u origin <old_name>Remove the new name from the remote
git push origin --delete <new_name>Pitfalls
- Open pull requests from the old branch name are closed when the old remote branch is deleted; rename before opening a PR or retarget it first.
- Skip the remote steps for a branch that was never pushed.