How to Rename Branch in Git
Git branches are helpful because they enable developers to work on different features without affecting each other’s work. In this article, we will learn how to rename local and remote Git branches, in case we misname one or want our project to be better organized within a team.
Rename a Local Git Branch
Let’s say we are working on our project and name a branch feature, but later on, we realize that we need to rename it to release or another name. We will rename it by using the rename subcommand and passing it the old and new names, like so:
git branch -m feature release
In detail we can say, make sure we have selected the branch we want to rename. We will run this command to do the desired work.
git checkout old-name
So, we have to replace the old name with the new name of the desired branch. If we want to see all of our local branches as a list, then we have to run the following command:
git branch --list
When we have selected the right branch to rename, then follow these below-mentioned steps. When we use the Git branch command, add an -m option to the given line. Then, rename the branch by executing the following command.
git branch -m old-name **new**-name
Finally, execute this command to list all local Git branches and confirm that the renaming was done successfully:
git branch -a
Rename a Remote Git Branch
As we’ve seen, we can easily rename a local branch that is been set up on the personal computer already. However, we may want to rename a branch that’s already been published to a shared repository. Git doesn’t let us rename remote branches (even though technically we can change the remote branch name to anything we want).
So, when we have a branch whose name we want to change, we have to do a few extra steps to get it renamed as compared to the local branch.
First, we will have to create a new branch with the new name; then we will have to delete the old one. This is because remote branches are not actually on the server, only their names are on the server. We have to create and delete them locally in the personal space.
To create the new branch, we can use the -b
flag to specify an existing branch to base it off. Once we have created the new branch, we can delete the old one using the -d
flag and the remote branch name.
Let’s say we have a remote branch named feature
that we need to rename to release. we can do this by running
git push -d feature release
It will delete the remote feature branch and replace it with the local release branch. And when we pull from the shared repository, it will change to the remote release branch.
Rename a Git Branch in One Step
Luckily, Git has a one-step command for renaming a branch. It’s called git branch -r
. If we run it, it’ll rename the current branch to the new name we specify.
git branch -r feature release
This renames our current branch from feature to release. It also changes the local branch to master. This section has shown us how to rename local branches as well as remote branches.
However, we may notice that if we run the git branch, we still see the old name of the renamed branch. If we want to see the new name, we can use the git branch -r
command. This command shows all of our branches, including their old and new names:
git branch -r release * release master
This section has shown us how to rename local and remote branches. However, it was a bit strange that we had to first create a new branch and then delete the old one.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn