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

51 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

    2. Yes, Otherwise it’ll lead to creation of a new branch with old name.
      Anyhow, this whole article is not really about renaming.
      Its about creating a New-Name branch from an Existing one.
      Pushing the New-Name branch and –deleting the Old-Name branch.

      Like

    1. It means that you can then just write `git push` when on that branch to push to the correct remote branch. If you don’t do it you will likely just get a warning when pushing that will prompt you to do this exact step. 🙂

      Like

  5. Worked for me, with one caveat… I wanted to change from branch name “mvp” to “MVP”, and it wasn’t posible because git is not case sensitive. The work around I used was to change the original name to “mvp1” and then to “MVP”. Thanks a lot, god bless.

    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