Over the course of the past week, I ran into a situation where I needed to rename a git branch which I had already pushed. Thanks to the internet, I was able to figure out exactly how to get this done.

In this note, I would be documenting what was done, and how you (future me 😀) can get it done too.


Just need the commands to run?

Fine, here…

# update local
git checkout <old_branch_name> && git branch -m <new_branch_name>
# update remote
git push origin -u <new_branch_name> && git push origin -d <old_branch_name>

With the straight to the point answer provided, let’s go into some detail.

Setting the Scene

Say you are working on a bug fix, and per team convention, bug fix branches are to have a name structure of bg/short-title-JIRA-NO, for example…


… and you’ve implemented the fix, tested and pushed.

It’s past 6 at this point and you are preparing to leave your workstation, but then you notice you messed up the ticket number – instead of TY-421, your branch name has TY-411.

“Arrgh!!!”, you scream (internally, of course).

Totally acceptable reaction, dear.

Totally acceptable.

I mean, what the hell?!

Let’s fix this, shall we?

Setting Things Right

The goal now is to rename the branch from bg/incorrect-timestamp-TY-411 to bg/incorrect-timestamp-TY-421 in all necessary locations. And this is what we would be doing now. Let’s fix up our local branch.

Rename Local Branch

Renaming the local branch is pretty straight forward. Here’s how:

First, ensure you’ve checked out to the “old branch”. In our case, this would be:

git checkout bg/incorrect-timestamp-TY-411

Then, rename the branch

git branch -m bg/incorrect-timestamp-TY-421

Updating Remote

Similar to how simple it was to update the local branch, updating remote also involves two easy steps.

First, push the renamed branch to origin (or whatever your upstream name is) and track:

git push origin -u bg/incorrect-timestamp-TY-421

Then, make the old origin branch redundant:

git push origin -d bg/incorrect-timestamp-TY-411

Summing up

With all of these done, things are right now, you can shut down and head home in peace 😌