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

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

  1. I think you should add a “git pull” at the top of these instructions.
    If you are missing commits and you push it, then the old branch will of course disappear, but so will any commits you didn’t have.

    Like

  2. In order to get this to work I had to add an additional step between steps 1 and 2:

    1a. Update Git config to point to new target.

    git config branch.new-name.merge refs/heads/new-name

    Without this step the old remote will fail to delete, and when you push the new name it will link it to the old remote branch. Even if you delete the old remote branch, Git will still incorrectly re-create it with the old name.

    git version 2.17.0 running under CygWin

    Like

  3. hi, it worked perfectly on the local branch, but when I tried to do this: git push origin :old-name new-name .., I got this: error: unable to delete ‘ft-create-user-160008818’: remote ref does not exist error: failed to push some refs to

    Like

    1. Sorry guys, everything works. apparently the name of the Github branch was missing a character. It works perfectly. Thanx

      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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s