How to Rename Git Branches

You type fast. You want to add one more function, one more docstring, one more test. You run ‘make test’. You see all tests green. You are ready to commit and push the changes and finally start the weekend (yes, it’s late Friday afternoon and your colleagues are about to start production deployment). 

Oh, you suddenly realise that you misnamed the git branch! What’s more, you pushed it to the remote repository!

For many of us, this story sounds familiar. What can we do? In this situation, time is crucial. Before any of your colleagues jump on you, you need to fix local and remote branch names. Let’s roll up our sleeves and resolve the issue. Here is what we will do:

Rename local branch

Let’s assume we are still on the same branch we want to rename:

If we are already on a different branch, the command looks like this:

Delete the old name remote branch and push the new name local branch

Reset the upstream branch for the new name local branch

Switch to the branch, and after that:

Alternatively we can use following 3 steps:

Rename branch locally

Delete the old branch

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

Off we go! Time for a Friday Pint 😉

Leave a reply:

This site uses Akismet to reduce spam. Learn how your comment data is processed.