How to Rename Local and Remote Git Branches

Abstract image used to represent How to Rename Local and Remote Git Branches
Image by hati.royani.

Maybe you're working in a Git repo with a specific naming convention, and you misname the branch you're working on. Perhaps you've been hit by an unfortunate typo. Either way, you need to rename your local branch and maybe even on the remote. Here's how you do that.

To switch to the branch and rename it locally, run:

git checkout old-branch-name
git branch -m updated-branch-name

This renames your local copy of the current branch. You can also do this without first checking out the local branch as a oneliner like this:

git branch -m old-branch-name updated-branch-name

Before changing a shared remote name, agree the move with your collaborators. Check that your local branch includes the remote work you need to keep, that the new name is unused, and how branch protection, open pull requests and any deployment settings refer to the old name.

Push the new name and set its upstream first. If this fails, stop: the old remote branch should remain in place.

git push --set-upstream origin updated-branch-name

After a successful push, compare the local commit ID with the ID reported for the new remote branch:

git rev-parse updated-branch-name
git ls-remote --heads origin refs/heads/updated-branch-name

The two commit IDs must match. If the remote command returns nothing or reports a different ID, investigate before continuing. Once the replacement is verified and your collaborators and repository settings are ready for the move, delete the old remote name:

git push origin --delete old-branch-name

And you're done! You've:

  1. Renamed your local branch.
  2. Created and verified the replacement remote branch.
  3. Set the renamed local branch to track the new remote name.
  4. Deleted the old remote name after coordinating the move.

The separate checks take a little longer than a oneliner, but they leave the old remote name available until its replacement is ready. This changes branch references; it does not rewrite the commits themselves. Other clones and services may still need their tracking or configuration updated.


Looking for technical direction?

I support teams that need senior judgement on React, Next.js, headless CMS architecture, performance, migrations, and technical SEO.