Rename a local and remote branch in git

If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.

1. Rename your local branch.
If you are on the branch you want to rename:

git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:

git push origin -u new-name
Advertisements

29 thoughts on “Rename a local and remote branch in git”

  1. It might be worth clarifying that if the old branch was being referenced anywhere, the references won’t be updated since you’re technically deleting an old branch and making a new one, not just changing the name of the same branch!

    Like

  2. Awesome. Thanks for this. Love blogs that are straight to the point.

    However the image at the top did throw me off. I opened this link in a new tab and when I was quickly switching through my chrome tabs I kept switching through this because at a glance I thought it was a random food blog I had open.

    Like

  3. I am following the steps mentioned above, but when I push the local renamed branch to the remote git repository, the old name appears again. I appreciate any help! Thanks.

    Like

  4. What if other devs have already checked out your old-name branch? Should they just delete the old-name branch and check out the new-name branch?

    Like

    1. They will be told that the upstream branch doesn’t exist (I think) and then they can either push their old-name branch back up as the old-name OR create a pull request for you to merge their branch into the new-name branch. This is where it gets MESSY! Hopefully you can just change the name quickly without this happening. 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s