To rename a Git branch, run the following command: git branch -m <old> <new>. This will change the name of the branch you are viewing to the new name you specify. You do not need to specify the old branch name if you want to rename the branch you are viewing.
How to Rename a Git Branch
Have you ever been in a position where the name of a branch is no longer relevant? Have you noticed a typo in the names of one of your branches that you just can’t stop thinking about? There’s good news for you: Git allows you to rename a branch.
In this tutorial, we’re going to talk about how to rename a Git branch. We’ll walk though an example of renaming a local and remote branch to help you get started.
Without further ado, let’s begin!
Git Branches
Branches are independent lines of development in a Git repository. They are used to separate your code. A branch lets you work on different parts of a project without having to affect the main line of development.
What happens if a branch is named incorrectly? Do you need to delete it? No. Git may be annoying at times but it does have a method that you can use to rename a branch. You can learn more about Git branches in our git branch tutorial.
Git Rename Branch
The git branch command lets you rename a branch. To rename a branch, run git branch -m <old> <new>. “old” is the name of the branch you want to rename and “new” is the new name for the branch.
Here is the syntax for the Git rename branch command:
git branch -m <old> <new>
Git Rename Local Branch Example
Let’s rename a Git branch. The syntax for renaming a branch is different depending on whether you are viewing the branch you want to rename.
If you want to rename the branch you are viewing, you only need to specify a new name for the branch. Otherwise, you need to specify the name of the branch you want to rename and the new name for the branch.
Let’s assume we want to rename a branch we are viewing. First, you’ve got to navigate to the branch that you want to rename. You can do this using the git checkout command:
git checkout fix-bug-22
This command allows us to view the branch fix-bug-22. If we wanted to view the master branch, we could run “git checkout master”.
We’ve just noticed that the fix-bug-22 branch is actually for fixing bug 23, not bug 22. Thus, we want to change the name of our branch. We can rectify this mistake by using the git branch command with the -m flag. This command lets us rename the branch:
git branch -m fix-bug-23<
Our branch name is now fix-bug-23. We can check if the name of the branch has changed using the git branch command:
git branch
Our command returns:
* fix-bug-23 master
We can see our “fix-bug-22” branch on our Git version control system has been renamed to “fix-big-23”.
We could skip the first step of moving to a branch. To do so, we should specify the name of the branch we want to rename when using the git branch -m command. If we do this, we do not need to navigate to a branch before renaming it. Consider this command:
git branch -m fix-bug-22 fix-bug-23
This command will rename our fix-bug-22 branch to fix-bug-23, no matter what branch we are presently viewing. We’ve just renamed a local branch.
Rename Branch Git: Remove Branch
You need to push a new version of a branch to a remote repository if you want to rename a remote branch. A remote branch is a branch associated with a remote repository, or a repository that is not stored on your local computer.
The first step is to rename a branch using the instructions from our last example. Then, you can push the new branch to your remote repository using git push:
git push origin -u fix-bug-23
This adds the branch fix-bug-23 to the remote repository with the name “origin”.
When you view your remote repository, the new branch will be visible. However, the old branch will still be visible. To fix this, you should delete the old branch from your remote repository using the git command line:
git push origin --delete fix-bug-22
This will delete the branch fix-bug-22 from your remote repository. Once you run that command, you’ll have a new branch called fix-bug-23 on your master, and no branch called fix-bug-22.
Conclusion
To rename a git branch, you can use the git branch -m command. The syntax for renaming a git branch is: git branch -m <old> <new>. You do not need to specify an old branch name if you are renaming the branch you are presently viewing.
Local and remote Git branches help developers keep their code separate from the main version of a project. Branches are useful because they allow developers to work on multiple different features and bug reports without changing the main line of development.
Do you want to learn more about Git? Check out our complete How to Learn Git guide which features top Git learning resources and courses.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot