How to Rename Local Branch in Git

Abdul Jabbar Feb 02, 2024
How to Rename Local Branch in Git

Git is a well-known, in-demand, and popular Version Control System (VCS) that software developers commonly use to collaborate in developing codes within small or large teams. It facilitates us in coordinating our work and helps us track changes in our source code during the software development process.

Git branches play a very essential and distinct role in developing our project within a team environment. Branches provide us a track to work along with our main (master) branch while keeping it free of mess and incomplete code in separate branches.

At times we are in a rush while developing branches and face a situation where we named the branch incorrectly, which doesn’t match or describe our code and seems very inaccurate. So then, we want to rename the branch.

Mostly, renaming the branches is due to the situation mentioned above. So, in this tutorial, we will learn to change the name of a local Git branch using the techniques mentioned below.

Rename Local Branch in Git

In Git, if we want to rename a branch, we will follow the below-mentioned steps to achieve our goal.

  1. Before we start, we should be sure that we have selected the branch that wants to rename by entering the name of the branch through the following command.

    git checkout old-name
    
  1. After selecting the right branch, we will use the command git branch with the alias -m.

    git branch -m new-name
    

    Or, we can also use a single command. If we are not already present in the master, we will switch to it.

    git checkout master
    
  2. After running the above checkout command, we will run the following command to change the branch name.

    git branch -m old-name new-name
    

    Now, our branch name has been changed.

  3. To verify whether the name has changed or not, we will recheck it by running the following command.

    git branch -a
    

    We’ll run the following command if we want to see all of our local branches with their correct names.

    git branch --list
    

From the above Git commands, we have seen that we can easily change our local branch name from old to new without any issue.

If you face any situation to change the name of the local branch for any reason, you can follow the steps mentioned above and change the name successfully.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

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

Related Article - Git Rename