How to Rename a Branch Locally and Remote in Git

More git tricks.

This took some googling, so I thought I would document my findings here.

Locally

If you’re already on the branch you’d like to rename, just run:

git branch -m new-name

If you’re on say master, and you have a branch you want to rename now but no checkout, run:

git branch -m old-name new-name

Now that you’ve taken care of your local branch, there’s the unfinished business of mending your remote branch with renaming.

Remote

If you use Github or Gitlab (or whatever) you’re going to want to remove the old-name branch and push the new-name branch:

git push origin :old-name new-name

And finally, push to reset the upstream branch so your local and remote branches are up-to-date:

git push origin -u new-name