How to Rename a Git Branch

When you work in Git, it’s important to know how to rename a Git branch to keep your projects consistent. Here’s how to do it in both a local repository and remote.

Written by Ayush Poddar
Published on Jun. 12, 2024
A software developer coding and adjusting a flow chart overlay
Image: Shutterstock / Built In
Brand Studio Logo

Whenever you work on a new feature in Git, it may go through different name changes even as it gets deeper into development. It’s important to know how to rename a Git branch to keep things consistent. 

Git Branch Rename Explained

Renaming a Git branch is an important step to ensure consistent naming of your features as you make changes in development. You can rename a Git branch with the following command line: git branch --move .

For example, you may be working on a feature named good-feature. As you work on it, an inspiration strikes and you decide to rename your feature as unicorn-feature. You’ll want to rename your feature branch from good-feature to unicorn-feature for consistency. However, you might already be deep into development and pushed quite a few changes to remote.

In this post, I will tell you:

  1. How to rename a branch in your local Git repository.
  2. How to rename the corresponding branch in remote.

More on GitHow to Undo the Last Commit Using Git Reset Command

 

How to Rename a Local Git Branch

The command is:

git branch --move <old-name> <new-name>

In our example, the command would be:

git branch --move good-feature unicorn-feature

A tutorial on how to rename a Git branch. | video: GitKraken

More on GitHow to Fix the GitHub Error ‘Remote Host Identification Has Changed’

 

How to Rename a Git Branch in Remote

To rename the Git branch in remote, you simply treat the branch with the new name as a new branch and push the branch to remote.

git push --set-upstream origin <new-name>

In our case, the command would be:

git push --set-upstream origin unicorn-feature

This will create a new branch in our remote (origin) named unicorn-feature. However, the old branch still remains in remote. To verify, you can run the git branch --all to list all the branches - including the ones in remote. The next step, then, is to delete the old branch (good-feature) from remote.

If you need to delete an old branch, you’d enter:

git push <remoteName> --delete <gitReference>

And that’s everything you need to know about renaming a git branch.

Frequently Asked Questions

You can rename a local git branch using the command: git branch --move <old-name> <new-name>.

You can rename a Git branch in remote with the command: git push --set-upstream origin <new-name>.

Explore Job Matches.