How to Rename an Existing Branch in Git

Git is a powerful version control system, and sometimes you may need to rename a branch—either to correct a typo, reflect a new naming convention, or simply organize your branches better.

In this guide, we’ll walk through how to rename a Git branch both locally and on remote repositories like GitHub or GitLab.


🔄 Rename a Branch Locally

✅ Rename the Current Branch

If you’re on the branch you want to rename, use:

git branch -m new-branch-name

✅ Rename a Different Branch

If you’re not on the branch you want to rename:

git branch -m old-branch-name new-branch-name

-m stands for “move,” which is Git’s way of renaming.


☁️ Push the Renamed Branch to Remote

After renaming locally, push the new branch to your remote:

git push origin new-branch-name

Then, delete the old branch from the remote:

git push origin --delete old-branch-name

🔁 Update Remote Tracking Branch

If you renamed a branch and want to continue tracking it remotely, set the upstream:

git push --set-upstream origin new-branch-name

🧠 Summary

TaskCommand
Rename current branchgit branch -m new-name
Rename another branchgit branch -m old-name new-name
Push new branch to remotegit push origin new-name
Delete old remote branchgit push origin --delete old-name
Set upstream for new branchgit push --set-upstream origin new-name

🚀 Final Thoughts

Renaming branches is straightforward with Git, but remember: if the branch has been pushed and used by others, communicate the change to your team to avoid confusion. Always verify that your new branch name follows any existing naming conventions (like feature/, bugfix/, etc.).

Sharing Is Caring:

Leave a Comment