Sometimes people get in a position where the name of their Git branch is no longer relevant. Or you have noticed a typo in one of your branches and now you can’t stop thinking about it. But don’t worry we have good news for you because you can easily rename a branch in Git. In this blog, we will talk about how to rename a git branch. We will go through an example of renaming a local and remote branch to help you understand how to rename a git branch. So, without any more delay let’s jump into action right now. Let’s begin.

How to rename a git branch

Also Read: DigitalOcean vs AWS vs Google Cloud vs Linode vs Vultr: Which is Best for Hosting WordPress?

Git branches

The branches in Git are independent lines of development in a Git repository. The Git branches are used to separate your code. A Git branch will help you work on different portions of a project without having any effect on the mainline of development.

So, what happens when your rename a Git branch incorrectly? Do you need to delete it? No, you don’t have to delete it. Git may be frustrating at times but it does have a method that you use to rename a branch easily.

How to rename a git branch

The git command lets your rename a branch. To rename a branch, run the Git branch command -m <old> <new>. Where <old> is the name of the branch you want to change and <new> is the new name for the branch.

Given below is the syntax for the Git rename branch command:

git branch -m &lt;old&gt; &lt;new&gt;

How to rename a git branch

How to rename a git branch

So, let’s rename a Git branch. The syntax may differ depending on whether you are viewing the branch you want to rename.

In the case of viewing the branch you want to rename you only need to provide a new name for the branch. In the case where you are not viewing the branch you need to provide the name of the branch and the name of the branch you want to rename.

Firstly, let’s take an example in the case where you are viewing the branch. In this case you have to go to the branch you want to rename. You can do that using the git checkout command:

git checkout fix-bug-22

The command given above will help you view the branch fix-bug-22. Also, if you want to view the master branch you could run the command “git checkout master”.

We just see that the fix-bug-22 branch is actually for fixing bug 23 and not bug 22. Thus if you want to change the name of the branch you can rectify the mistake by using the git branch command with the -m flag. This command let’s us rename the branch.

git branch -m fix-bug-23<

Now as you can see the new branch name is now fix-bug-23. You can also check if the name of the branch has changed using the git command given below.

git branch

The above command returns the answer:

* fix-bug-23
master

Now you can see that our “fix-bug-22” branch on our Git version control system has been renamed to “fix-bug-23”.

So, now if you want to rename a branch without viewing it you can easily do that to. In order to do that all you have to do is specify the name of the branch you want to rename when using the git branch -m command. If you do this you don’t need to navigate to a branch before changing its name. The command is given below:

git branch -m fix-bug-22 fix-bug-23

What this command will do is rename your fix-bug-22 branch to fix-bug-23. Not taking into consideration which branch you are currently viewing. And you have renamed a local branch easily.

Rename Branch Git: Remove Branch

You have 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 of a remote repository or a repository that you have stored in your computer.

Firstly, you need to take instructions from our last example to rename a branch. Then in the next step you need to push the new branch to your remote repository using the Git push.

git push origin -u fix-bug-23

What this will do is add the branch fix-bug-23 to the remote repository with the name “origin”.

Now when you view your remote repository you can see the new repository. But the old branch will still be visible. In order to fix the issue you need to delete the old branch from your remote repository using the git command given below.

git push origin --delete fix-bug-22

Now this will delete the branch fix-bug-22 from you local repository. Once you run the command you will have a new branch called fix-bug-23 on your master and no other branch called fix-bug-22.

Conclusion:

Here is how to change the name of a git branch. As branches are useful part of the Git because they allow developers to work on multiple different features and bug reports without changing the main line of development. Hope you find this information useful. Thank you for the read.

Categorized in:

Tagged in: