Home » How To Rename Git Branch Both Locally And Remotely

How To Rename Git Branch Both Locally And Remotely

Last updated on December 24, 2020 by

Did you mistakenly give a wrong branch name? Do you want to give a meaningful name using git commands? Yes, Then you are at the perfect place. In this article, we will show how to rename the Git branch both locally and remotely. Let’s just jump into it.

Table of Contents
1. Renaming a Local Git Branch
2. Renaming a Remote Git Branch

Rename Git Branch

01 Renaming a Local Git Branch

CASE 1: If you are on a current branch and you want to change the current branch name then run the following command:

git branch -m new-branch-name

The -m flag is abbreviated form of --move, you can use either -m or --move.

You can also use the capital -M flag which means --move --force, allows you to forcefully rename even if the branch already exists. So the existing branch will be overridden by it.

CASE 2: If you are on a different branch and you want to change any other branch name then run the following command:

git branch -m old-branch-name new-branch-name

Example:

Let’s say we have a branch ‘list-view’ in local and want to change it’s name to ‘grid-view’ then you can do it with following steps:

1. Let’s first check for the available branches using git branch command

$ git branch
  list-view
* master

2. As we can see above we have 2 branches master and list-view. The * denotes that we are on branch but we want to change the list-view branch name then we have to go with CASE 2. Let’s now rename the branch from list-view to grid-view.

$ git branch -m list-view grid-view

3. That’s it. Let’s run the git branch command again and verify the change.

$ git branch
  grid-view
* master

We can see that git branch now listing the new branch name "grid-view". That means it’s working correctly.

02 Renaming a Remote Git Branch

If you’ve already pushed the Git branch to the remote repository then you can perform the following steps to rename a remote branch.

1. Delete the branch with the old name on the remote repository:

git push origin --delete old-branch-name

2. Finally, push the branch with the correct name:

git push origin :old-branch-name new-branch-name

That’s it. You have successfully renamed both local and remote Git branches.

Additionally, read our guide:

  1. Basic GIT Commands For Beginners With Project Set-Up
  2. How To Undo Last Commit In Git
  3. How To Delete A Git Branch Both Locally And Remotely
  4. How To Undo Add File In Git Before Commit
  5. Difference Between == vs === in JavaScript
  6. How To Remove A Specific Item From An Array In JavaScript
  7. How To Check Array Contains A Value In JavaScript
  8. How To Merge Objects In Vue

That’s it from our end. We hope this article helped you to rename the git branch both locally and remotely.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment