Rename Git Branch (Local and Remote)
Commands to rename a local and remote Git branch including updating tracking references.
# Rename current branch
git branch -m new-name
# Rename a different branch
git branch -m old-name new-name
# Update the remote (delete old, push new)
git push origin --delete old-name
git push origin new-name
# Reset upstream tracking
git push --set-upstream origin new-name
# Verify the rename
git branch -a
# If other team members have the old branch checked out:
# They need to run:
git fetch --all --prune
git checkout new-name
git branch -D old-nameUse Cases
- Fixing branch naming convention mistakes
- Renaming default branch from master to main
- Updating branch names to match ticket IDs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Delete Remote Git Branch
Commands to delete local and remote branches, prune stale references, and clean up merged branches.
Git Force Push Safely
Use force-with-lease instead of force push to prevent overwriting teammates' commits on remote.
Git Clone Specific Branch
Clone a specific branch from a remote repository without fetching all branches and history.
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.