Feel the fear and do it anyway

Susan Jeffers

How to rename a git branch

Rename local branch #

To rename a local branch in git

  • Move on the branch you want to rename
git checkout -b feature/wrong-name
  • Rename it locally
git branch -m feature/new-awesome-name

⚡️ Bonus tip #

If you have ohmyzsh git plugin installed, you can use its shortcuts

gco -b feature/wrong-name
gbm feature/new-awesome-name

Rename remote branch #

To rename a remote branch is quite longer:

  • Unset the upstream branch to unlink local and remote branch
git branch --unset-upstream

Note: if you followed the previous step, you don't have to delete local branch because you have already renamed it!

  • Update the upstream branch to the new one and push it
git push --set-upstream origin feature/new-awesome-name

⚡️ Bonus tip #

If you have ohmyzsh git plugin installed, you can use its shortcut to set the upstream

gpsup (branch name is implicit)
  • Delete remote branch
git push origin --delete feature/wrong-name

or the shortest syntax (note the space between remote name and semicolon)

git push origin :feature/wrong-name
Edit this page on GitHub

Comment on DEV or Hashnode

Take me to the next post!