How to Rename Remote Branch in Git: Simple Steps
To rename a remote branch in Git, first rename your local branch using
git branch -m old-name new-name. Then delete the old remote branch with git push origin --delete old-name and push the new branch with git push origin new-name. Finally, reset the upstream branch with git push --set-upstream origin new-name.Syntax
Here are the main commands to rename a remote branch in Git:
git branch -m old-name new-name: Rename your local branch.git push origin --delete old-name: Remove the old branch from the remote repository.git push origin new-name: Push the renamed branch to the remote.git push --set-upstream origin new-name: Link your local branch to the new remote branch.
bash
git branch -m old-name new-name git push origin --delete old-name git push origin new-name git push --set-upstream origin new-name
Example
This example shows how to rename a remote branch called feature1 to feature-renamed.
bash
git checkout feature1 # Rename local branch git branch -m feature1 feature-renamed # Delete old remote branch git push origin --delete feature1 # Push new branch to remote git push origin feature-renamed # Set upstream to track remote branch git push --set-upstream origin feature-renamed
Output
Switched to branch 'feature1'
Deleted branch feature1 (was abc1234).
To https://github.com/user/repo.git
- [deleted] feature1
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/user/repo.git
* [new branch] feature-renamed -> feature-renamed
Branch 'feature-renamed' set up to track remote branch 'feature-renamed' from 'origin'.
Common Pitfalls
Common mistakes when renaming remote branches include:
- Not deleting the old remote branch, which leaves stale branches on the server.
- Forgetting to set the upstream branch, causing push and pull commands to fail.
- Trying to rename a branch without switching to it first.
Always ensure you are on the branch you want to rename locally before running git branch -m.
bash
git branch -m new-name # Wrong if not on the branch to rename # Correct way: git checkout old-name git branch -m new-name
Quick Reference
| Command | Purpose |
|---|---|
| git branch -m old-name new-name | Rename local branch |
| git push origin --delete old-name | Delete old remote branch |
| git push origin new-name | Push renamed branch to remote |
| git push --set-upstream origin new-name | Set upstream tracking for new branch |
Key Takeaways
Rename your local branch first using git branch -m.
Delete the old remote branch to avoid confusion.
Push the renamed branch and set upstream tracking.
Always switch to the branch before renaming locally.
Check remote branches with git branch -r to confirm changes.