How to Rename your Local Branch in Git

How to rename your local branch in Git is just one command. But the sole reason I have written this post is because I keep forgetting. There have been moments when I would start working on a branch giving a much relevant name to the task at hand, but suddenly pick up something else. So the work I do, and the name I use end up being not in sync.

Such times I need to rename my branch, you know, just so, it says what it says it is. And every time I want to change the name of the branch, I forget how to rename it. That’s when I have to either google it or refer to my notes. A better place would be here. So all I have to do is open my site, and search “rename your local branch in git” in the search box.

That’s it! and this post would flare up.

branch manager meme

Just so you guys don’t face a similar problem, I have decided to place it here. Alternatively, I am thinking about creating one post that will house all the commands that you might use in git. But that would be overwhelming for newbies.

This one approach of learning is where things seem simpler and you get a good feeling of having moved a mountain even though it is a small one. So without wasting any more time, let’s get straight to the command to be used.

How to Rename Local Branch in Git

So firstly, I am assuming you have your git terminal open, and you are already inside the repository where you are working as was explained in all our previous git tutorials.

Step 1: You can simply do that by doing a right-click git bash on the repository location in your local.

git bash here option

Step 2: When the terminal opens you will can be sent on your master or whatever the branch you are working on your editor.

git bash terminal opens

Renaming a Local Branch from Any Other Branch

Step 3: If you want to rename a branch irrespective of the branch you are on, you can do a simple:

git branch -m <oldName> <newName>

Then press enter. That’s it. That should change the name of the branch you want.

Example

Let us see this using an example:

The branch whose name I want to change is called “stuff”. The new name that I want to give it is “newStuff”.

Then I should be using the following command:

git branch -m stuff newStuff

and press Enter.

git branch rename

Pressing Enter would still keep you at the branch you were at. So you gotta navigate to the newly created branch. To do that just do a:

git checkout newStuff

When you execute the above command you would notice that indeed the branch name has been changed.

branch name changed

Renaming your Current Local Branch in Git

The better alternative I would say is this. Because you just have to navigate at the branch you are working on. That’s all, and run just one command. It will change the name of your branch.

Step 1: If you want to change the name of the current branch you are on, then first navigate to that branch using:

git checkout <oldBranch>

Step 2: Then when you are on the branch whose name you wish to change, do a simple:

git branch -m <newBranchName>

where <newBranchName> is the name you want to replace the existing branch name with.

Let us see this using an example now:

Example:

For e.g. I want to change the name of my branch “oldBranch” to “newBranch”.

I am assuming we are on the master. So I will navigate to the oldBranch by using this first.

git checkout oldBranch

As you can see we have navigated to the branch oldBranch with the help of checkout.

How to rename your local branch in Git checkout

Time to execute the rename branch command now.

git branch -m newBranch

where newBranch is the new branch name.

branch name in git

NOTE: It is totally up to you how to name your branch. I prefer using camel cases just so to avoid extra overheads of other cases like using hyphens or underscores. Again, this is completely up to you, and a matter of personal choice.

Make sure to delete your branches when all the work has been done, and your branches have been merged with the origin.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply