Rename a branch with git branch -m, push the new name, delete the old one on the remote and fix the upstream, so you and your team end up on the same branch.
The short answer
#Git has no single command that renames a branch everywhere. Rename it locally with git branch -m, push the new name and set it as the upstream, then delete the old name on the remote.
_10git branch -m old-name new-name # rename locally_10git push -u origin new-name # push and track the new name_10git push origin --delete old-name # remove the old name from the remote
Why it happens
#A branch is just a named pointer to a commit. Your local branch and the branch on the remote are two separate pointers that happen to share a name, and your local branch remembers which remote branch it tracks (its upstream).
git branch -m only renames your local pointer. The remote still has the old name, and your local branch still tracks origin/old-name. That is why you need the extra push and delete steps, and why teammates have to update their own clones.
How to fix it
#Rename the branch locally
If you are on the branch you want to rename, give only the new name:
_10git switch feature/login_10git branch -m feature/sign-in
From any other branch, give both names:
_10git branch -m feature/login feature/sign-in
If a branch with the new name already exists, Git refuses. -M forces the rename and overwrites the existing branch, so check first with git branch --list. -M is also the flag to use for a change that only alters letter case, such as Feature to feature, on case-insensitive file systems like the macOS default.
Update the remote
Push the renamed branch and set its upstream in one go:
_10git push -u origin feature/sign-in
Then delete the old branch on the remote:
_10git push origin --delete feature/login
Check the result with git branch -vv, which shows each local branch and the remote branch it tracks.
Use your hosting service for shared branches
For a branch other people use, especially a default branch such as master to main, the web interface of services like GitHub and GitLab can rename it for you. The service then updates things that point at the branch, such as open pull requests and branch protection rules, which a manual delete and push does not. After renaming there, update your local clone as below.
Update a teammate's clone
Anyone who already has the branch checked out needs to rename theirs and point it at the new upstream:
_10git fetch origin --prune # get the new name, drop the old one_10git branch -m feature/login feature/sign-in_10git branch -u origin/feature/sign-in feature/sign-in
If the renamed branch was the remote's default branch, also refresh the remembered default:
_10git remote set-head origin --auto
Common pitfalls
#- Renaming locally and stopping there. The next
git pusheither fails or goes to the old remote name, because the upstream still points at it. - Deleting the old remote branch while pull requests use it. Many services close or orphan those pull requests. Rename through the web interface, or retarget them first.
- Forgetting CI configuration, deploy settings or branch protection rules that name the old branch.
- Using
-Mwithout checking for an existing branch with the new name. That branch is replaced. - Expecting
git fetchalone to remove the old remote-tracking branch. Add--prune, or setgit config --global fetch.prune true.
Related reading
#Was this resource helpful?