Quick answer: to rename the branch you're currently on:
git branch -m new-name
To rename a branch you're not currently on:
git branch -m old-name new-name
Renaming a branch already pushed to a remote
Local renames don't automatically update the remote. If the old name already exists on
origin, do this after renaming locally:
git push origin -u new-name # push the new name, set upstream
git push origin --delete old-name # remove the old remote branch
Case-only renames on Windows/macOS
On case-insensitive filesystems, renaming Feature to feature can fail with
"branch already exists." Force it with a capital -M:
git branch -M new-name
Handy alias
git config --global alias.rename "branch -m"
git rename new-name
FAQ
Does renaming a branch rewrite commit history?
No. Only the branch label changes; every commit SHA stays identical, so it's completely safe even on shared branches (aside from needing to update the remote name as shown above).
Will my teammates automatically see the new branch name?
No. Each teammate needs to fetch/prune and check out the new name themselves; Git doesn't broadcast local renames.
What's the difference between -m and -M?
-m refuses to overwrite an existing branch name; -M forces the rename even if a
branch with the new name already exists.
This article explains and expands on the community answers to the Stack Overflow question “How can I rename a local Git branch?”, used under the CC BY-SA 4.0 license. Screenshot credit: Stack Exchange Inc.
No comments
Post a Comment