Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
rename git branch locally and remotely
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
Copy link

bhatrajiv7619 commented Dec 27, 2019

This works like charm provided there are atleast 2 branchs. In my sample scenario, i had only one branch named "Dev". I was trying to change it to "master".

i got this error - " ! [remote rejected] Dev (refusing to delete the current branch: refs/heads/Dev) error: failed to push some refs to '..URL'"

As someone suggested, i had to do this 'git push -u origin :Dev master'. Then delete the Dev branch.

Copy link

kamalsomu commented Jan 13, 2020

@mitendraanand Thanks... :)

Copy link

StaroKep commented Jan 16, 2020

Oh la la! Thanks 🙏

Copy link

matchbox10 commented Jan 27, 2020

That'll do!

Copy link

YimmyRengifoLaHaus commented Mar 10, 2020

Thank you ! :)

Copy link

andresdhn commented Apr 30, 2020


Copy link

razziemix commented Jun 10, 2020

thank a lot!!

Copy link

vjpoint commented Jul 27, 2020

Copy link

BwayCarl commented Aug 1, 2020

This worked perfectly. Thank you.

Copy link

DEARaison commented Sep 24, 2020

It worked perfectly. Thank you.

Copy link

shravan-prajapati commented Oct 7, 2020

Thanks It is working.

Copy link

jogaec22 commented Oct 14, 2020

Thanks, it helped me!

Copy link

DmMhs commented Oct 15, 2020

@chrisivens you've saved my day. Thanks

Copy link

murtajaziad commented Oct 16, 2020

Thanks a lot 🌹

Copy link

devinrhode2 commented Nov 5, 2020

I have a better alias, that only requires you to specify the new branch name you want.

  rename = "!moveit() { git push origin --delete `git branch --show-current` || true; git branch -m $1; git push --set-upstream origin $1; }; moveit"

It tries to delete your current branch remotely first, but continues on if this fails. Then renames it locally, and then pushes the newly named branch.

It may be problematic if you have stuff on your remote branch that you don't have locally, but, that's extremely unlikely, although you may need to dig into git reflog to find them in that edge case. If you want to preserve your old/current remote branch name, don't use this.

Usage - renames your current branch:

git rename new_branch_name

Copy link

aramodi commented Nov 21, 2020

First time I saw a gist with three lines having 2k stars lol

Copy link

sangeethkumarn commented Feb 18, 2021

Thank you so much

Copy link

Mohitmadhav commented May 12, 2021

Thanks a lot !! Worked smoothly

Copy link

SynCap commented Jul 28, 2021

PowerShell function/module for this

Copy link

vishalbandre commented Oct 5, 2021

Thanks, great. It works as expected.

Copy link

Rakesh-space commented Oct 28, 2021

Thanks you

Copy link

bidehalaxmi commented Jan 12, 2022

Thank you

Copy link

sunnycs121 commented Jan 31, 2022

Very helpful. Thank you so much!

Copy link

adedoyin-paystack commented Mar 22, 2022

Thank you very much

Copy link

HaSS-1 commented Mar 29, 2022

you pusted this solution 8 years ago ... but still working like a charm.

Copy link

Al-Mutawakkil commented Apr 11, 2022

Thank You so much!

Copy link

au-kirkwang commented Sep 8, 2022

Very helpful. Thank you so much!

Copy link

lorenzogrv commented Sep 14, 2022

Thanks! (again) ❤️

Copy link

Rayhan-khan-Ridoy commented Nov 3, 2022

thanks...its working

Copy link

deSp44 commented Nov 30, 2022


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment