How Can I Rename a Local Git Branch?

Better Stack Team
Updated on June 21, 2024

To rename a local Git branch, you can use the following steps:

Step 1: Checkout a New Branch

First, make sure you're not on the branch you want to rename:

 
git checkout <branch-to-rename>

Step 2: Rename the Branch

You can then rename the branch using the -m option of git branch:

 
git branch -m <new-branch-name>

Replace <new-branch-name> with the desired name for your branch.

Step 3: Push the Renamed Branch (Optional)

If the branch has already been pushed to a remote repository and others are using it, you'll likely want to push the changes to the remote repository as well. You can use the following command to do that:

 
git push origin -u <new-branch-name>

This command sets the upstream branch for the new branch and pushes it to the remote repository, effectively renaming the branch remotely as well.

Notes:

  • Renaming a branch is a local operation until you push the changes to the remote repository.
  • Renaming the branch locally will not automatically update any pull requests, issues, or references to the old branch name. You'll need to manually update those references if necessary.
  • If the branch is checked out, you cannot rename it directly. You need to checkout a different branch first, then rename the branch.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github