Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Rename a Local and Remote Git Branch

Rename a branch on your computer, then push the new name and delete the old one on the remote.

How-To Guides2 min read9 screenshots

Rename a local branch

Step 1: List the branches

git branch -a
Listing the available branches

Step 2: Switch to the branch

git checkout old_branch
Switching to the branch to rename

(If you are on another branch, you can also rename without switching: git branch -m old_branch new_branch.)

Step 3: Rename it

git branch -m old_branch new_branch
Renaming the local branch

If you are on the branch you want to rename, git branch -m new_branch is enough.

Step 4: Check

git branch
Verifying the rename

Rename the remote branch

Git cannot rename a branch on the remote. You push the new name and delete the old one.

Step 1: List the remote branches

git branch -r
Listing the remote branches

Step 2: Push the renamed branch

git push origin -u new_branch
Pushing the renamed branch

-u sets the upstream so git push and git pull work without more arguments.

Step 3: Delete the old branch on the remote

git push origin --delete old_branch
Deleting the old remote branch

Step 4: Reset the upstream (if needed)

git branch --set-upstream-to=origin/new_branch new_branch
Resetting the upstream branch

Step 5: Clean up old references

git fetch --prune
Cleaning up the local references

Tell your team

Other people who have the old branch must update their copy:

git fetch --prune
git branch -m old_branch new_branch
git branch --set-upstream-to=origin/new_branch new_branch
Note: If the branch is the default branch (such as master to main), also change the default branch in your hosting settings (GitHub, GitLab) before you delete the old one. Open pull requests and CI settings may use the old name too.

Quick recap

  • git branch -m old new renames locally.
  • git push origin -u new and git push origin --delete old rename the remote.