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
Thank you!
LikeLike
Reblogged this on Connoisseur and commented:
Rename a local and remote branch in git
LikeLiked by 2 people
Thank You.
LikeLike
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!
LikeLiked by 1 person
I don’t really get the image at the top of this post, but this is totally informative. Thank you
LikeLike
It represents an electron being in multiple states.
LikeLiked by 2 people
Thanks a bunch! This was really helpful!
LikeLike
Thank you
LikeLike
Thanks mate
LikeLike
Thank you
LikeLike
Reblogged this on Santak Dalai.
LikeLike
Thanks 🙂
LikeLike
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.
LikeLike
Doesn;t look like very nice food! 🙂 Sorry we are very abstract here at the studio. 🙂
LikeLike
can you tell us what that image is? grapes or baloons? Thanks
LikeLike
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.
LikeLike
Hmmm, not sure why that would happen. you could switch step 2 to the below and see how that goes.
git push origin --delete old-name
LikeLike
awesome! thank you!
LikeLike
Reblogged this on Mobile App Development.
LikeLike
Thank you!
LikeLike
Thanks! Clear and efficient
LikeLiked by 1 person
Very useful! Thanks
LikeLike
Laughed really hard at your intro scenario for why you might be renaming the branch (exactly why I did)
LikeLiked by 1 person
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?
LikeLike
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. 🙂
LikeLiked by 1 person
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.
LikeLike
Simple but amazing
LikeLike
great, concise instructions!
LikeLike
Thank you!
LikeLike
Thank you! Specific instructions
LikeLiked by 1 person
Thanks….
LikeLike
Worked like a charm!
Thank you!
LikeLike
Brilliant, Thanks!
LikeLike
Thanks!
What is the 3rd step for?
What happens if I don’t do it?
Thanks a lot!
LikeLike
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. 🙂
LikeLike
Thank you. It worked!
LikeLike
Thank you.
LikeLike
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.
LikeLike
Thanks!!
LikeLike
Thanks!!!
LikeLike
thanks
LikeLike
Thanks, helped me today!
LikeLike
Reblogged this on The Opportunist and commented:
I found this easy-to-read blog on renaming git branches and just had to share…
LikeLike
Reblogged this on sharepointswe.
LikeLike
Does “gitup” work instead of the longer command in step 3?
LikeLike
Thank you
LikeLike
Thanks a gillion!
LikeLike
Reblogged this on McDoyen.
LikeLike
Thanks.
This was very helpful an efficient
LikeLike
Thank you for this!
LikeLike
Thank you…
LikeLike
Thank You! steps were handy and worked well.
LikeLike
This was really helpful, thanks!
LikeLike
how do you add all the commit messages too seems like mine didnt add to the new renamed branch?
LikeLike
Thanks a lot, it helped!
LikeLike
Thank you so much
LikeLike