X

Renaming A Git Branch | Which Command Is Used To Rename A Branch In Git?

Renaming A Git Branch | Which Command Is Used To Rename A Branch In Git? 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. 

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. Below we are going to be talking about how to rename a Git branch.

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 &lt;old&gt; &lt;new&gt;

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.

How Rename branch git local and remote

Git Rename Local Branch Example

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.

The following command allows us to view the branch fix-bug-22. If we wanted to view the master branch, we could run “git checkout master”.

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 lets us rename the branch:

git branch -m fix-bug-23<

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.

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

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

Remove Branch: Rename Branch Git

You need to push a new version of a branch to a remote repository if you want to rename a remote branch. The first step is to rename a branch using the instructions from our last example.

git push origin -u fix-bug-23

Method 1: How to Rename a Branch in Git

Make sure you are in the root directory for your project using cd command.

You will first need to open up your terminal and then cd (change directory) to the root of your project. This is what the command would look like if you were in the home directory and wanted to cd into the project which is located on the Desktop.

cd Desktop/project-name

Go to the branch you want to rename using git checkout command

We can use the git checkout command to switch to another branch.

git checkout branch-name

Use the -m flag to change the name of the branch

This is what the command would look like to change the name of the branch:

git branch -m new-branch-name

If you want to change a branch name from test-branch to test-branch2. Use the command below.

git branch -m test-branch2

Method 2: Renaming A Git Branch

You can also rename the local branch in just one command without having to use git checkout.

Make sure you are in the master/main branch

To check if you are in the master/main branch, run git statusIf you are not in the master/main branch, then you will need to run git checkout master or git checkout main.

Use the -m flag to rename the branch

You can use this syntax to rename the old branch to something new.

git branch -m old-branch new-branch

This is what it would look like to rename the test-branch to test-branch2.

git branch -m test-branch test-branch2

Recommendation

This post was published on Jul 9, 2022 23:14

Categories: Tips & Guide
Kamerpower Content Team: Dedicated to producing the best educational resources, orientation to students, insightful career advice including guides and how-tos. Our team also help students apply to foreign universities, counsels students towards their higher education dream while ensuring they get the best employment guide and career advice to help them after studies.
X

GDPR

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings