bashbeginner
Rename Git Branch (Local and Remote)
Commands to rename a local and remote Git branch including updating tracking references.
bashPress ⌘/Ctrl + Shift + C to copy
# 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.
bashbeginner
Delete Remote Git Branch
Commands to delete local and remote branches, prune stale references, and clean up merged branches.
Best for: Cleaning up after merged pull requests
#git#branch
bashintermediate
Git Force Push Safely
Use force-with-lease instead of force push to prevent overwriting teammates' commits on remote.
Best for: Pushing after rebase without overwriting others' work
#git#force-push
bashbeginner
Git Clone Specific Branch
Clone a specific branch from a remote repository without fetching all branches and history.
Best for: Cloning only a deployment branch for CI/CD
#git#clone
bashbeginner
Git Remote Prune and Branch Cleanup
Clean up stale remote-tracking branches and local branches that have been merged or deleted upstream.
Best for: Keeping local repository tidy
#git#cleanup