How to Rename a Local Git Branch

Overview

You may find yourself in a position where you need to rename a local Git branch. This tutorial will show you how to do so.

Renaming a branch falls under the branch command of Git, and it is accomplished by using the Move/rename flag. When a branch is renamed or moved the reflog is also updated.

The Move/rename flag can be specified a number of ways.

git branch -m <current name> <new name>
git branch --move <current name> <new name>

Rename the Current Active Git Branch

To rename the current branch we use the move/rename flag, as mentioned earlier.

For example, if our current branch is named “add-comments”, but should have been named “feature/add-comments”, we simply just specify the branch’s new name with the rename flag.

git branch -m feature/add-comments

Using the long form variant of the move/rename flag

git branch --move feature/add-comments

Rename a Non-Active Git Branch

Renaming a non-active branch is similar to renaming the active branch. The difference is we must target the branch we want to name.

The following example renames a branch named “bad-branch-name” to “feature/add-comments”.

git branch -m bad-branch-name feature/add-comments

Updates Remote Repository

Our changes so far have been local only. In order for our changes to be applied to the remote branch we must perform two tasks. First we must push the update to the branch’s previous name, and secondly we push our newly named branch with its refs.

First step, push the branch to the remote using its previous name

git push origin :bad-branch-name

Then we push the branch with its new name

git push origin feature/add-comments:refs/heads/feature/add-comments