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.