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
thanks so much man. works like a charm
LikeLiked by 2 people
Thanks – nice and simple and clear
LikeLiked by 2 people
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.
LikeLike
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
LikeLiked by 1 person
Thanks
LikeLike
Thanks! Quick, to the point, effective.
LikeLike
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
LikeLike
Sorry guys, everything works. apparently the name of the Github branch was missing a character. It works perfectly. Thanx
LikeLike
Thanks for theme commands.
LikeLiked by 1 person
Thank You for this.
LikeLike
Thanks for this! Worked like a charm
LikeLiked by 1 person