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.