How to rename git branch locally and remotely

Most times I find myself, with the need to rename a git branch I am working on, due to various reasons.

By following this simple steps, I was able to rename my local and rename branch names:

  • Rename branch locally

    git branch -m old_branch new_branch

  • Delete the old branch

    git push origin :old_branch

    or if you have Git Version > 2.6, you can easily use this command instead.

    git push origin --delete old_branch

  • Push the new branch, set local branch to track the new remote

    git push --set-upstream origin new_branch

Note: Remember to pull before! Or you may lose every commit not yet on your local branch

See example below.

Git branch rename