How do I properly use git rename remote branch to fix a typo in the branch name?

I accidentally created a remote branch named regacy (instead of legacy) and my local master branch is currently tracking origin/regacy.

I want to correct this using the git rename remote branch approach, essentially renaming origin/regacy to origin/legacy or origin/master.

I tried running:

git remote rename regacy legacy
But it threw this error:
error: Could not rename config section 'remote.regacy' to 'remote.legacy'

What’s the correct way to rename a remote branch in Git without breaking the local tracking setup?

Any advice on how to safely do this and push the changes would be appreciated!

In Git, you can’t technically rename a remote branch like you would a local file, it’s more of a delete-and-push process.

To fix your typo, you’ll want to create a new branch with the correct name, push it, and then remove the old one:

bash
Copy
Edit
git checkout regacy
git branch -m legacy         # rename locally
git push origin legacy       # push new branch
git push origin --delete regacy  # delete the typo branch

After this, make sure to update any open pull requests or CI configs that were pointing to regacy.

Git doesn’t have a rename remote branch command per se, but this gets you the same result.

Since your local master is tracking origin/regacy, you’ll need to clean that up after renaming the remote.

After you create and push the corrected branch as mentioned above, update the tracking info on your local branch:

bash
Copy
Edit
git branch -u origin/legacy master

That tells your local master branch to start tracking origin/legacy instead of origin/regacy.

You’ll also want to prune your stale references:

bash
Copy
Edit
git remote prune origin

This makes sure your Git repo reflects that regacy is gone.

It’s a small step but helps avoid confusion when working with remotes later.

@Apurvaugale If your repo is part of a shared team setup, it’s worth checking who else might be using origin/regacy before you delete it.

One way I’ve handled this is by pushing the new legacy branch first and letting folks know it’s the correct one, before removing regacy entirely.

In cases like this, I also tag the last commit on the old branch (just in case):

bash
Copy
Edit
git tag backup/regacy origin/regacy
git push origin --tags

Then you can safely remove the old branch, and people have a reference point if needed.

It’s not just about renaming, it’s about not surprising your team or CI system with missing branches.