This guide explains how to rename a local and remote Git branch step by step. When collaborating on a project with a team, maintaining a consistent Git branch naming convention is essential. Clear and standardized branch names make collaboration smoother and help avoid confusion during development.
Sometimes, despite best intentions, you may create a branch with an incorrect name and even push it to the remote repository before noticing the mistake. Fortunately, Git makes it easy to rename both local and remote branches using simple commands.
Renaming a Git Branch
Follow the steps below to rename a local Git branch and update it on the remote repository.
Step 1: Switch to the Branch You Want to Rename
First, check out the branch that needs to be renamed:
git checkout <old_name>
This ensures you are working on the correct branch.
Step 2: Rename the Local Branch
Rename the local branch using the git branch -m command:
git branch -m <new_name>
At this point, the branch has been successfully renamed locally.
Step 3: Push the Renamed Branch to the Remote Repository
If the old branch name has already been pushed to the remote repository, you’ll need to update the remote as well.
Push the renamed branch and set it as the upstream branch:
git push origin -u <new_name>
This command pushes the new branch name to the remote repository and links it with the local branch.
Step 4: Delete the Old Remote Branch
Once the new branch is pushed, delete the old branch name from the remote repository:
git push origin --delete <old_name>
This removes the incorrectly named branch from the remote repository.
You have successfully renamed both the local and remote Git branch.
Conclusion
Branches are a core part of the software development process and one of Git’s most powerful features. A Git branch is essentially a pointer to a specific commit, allowing developers to work independently without affecting the main codebase.
Renaming a local Git branch is as simple as running a single command. However, because Git does not allow direct renaming of a remote branch, the process involves pushing the renamed branch and deleting the old one.
Keeping branch names clean and consistent helps improve collaboration and repository management.
If you encounter any issues or have feedback, feel free to leave a comment below.
