Skip to content

Instantly share code, notes, and snippets.

Embed
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
@bhatrajiv7619
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.

@kamalsomu
Copy link

kamalsomu commented Jan 13, 2020

@mitendraanand Thanks... :)

@StaroKep
Copy link

StaroKep commented Jan 16, 2020

Oh la la! Thanks 🙏

@matchbox10
Copy link

matchbox10 commented Jan 27, 2020

That'll do!

@YimmyRengifoLaHaus
Copy link

YimmyRengifoLaHaus commented Mar 10, 2020

Thank you ! :)

@andresdhn
Copy link

andresdhn commented Apr 30, 2020

Legend!

@razziemix
Copy link

razziemix commented Jun 10, 2020

thank a lot!!

@vjpoint
Copy link

vjpoint commented Jul 27, 2020

@BwayCarl
Copy link

BwayCarl commented Aug 1, 2020

This worked perfectly. Thank you.

@DEARaison
Copy link

DEARaison commented Sep 24, 2020

It worked perfectly. Thank you.

@shravan-prajapati
Copy link

shravan-prajapati commented Oct 7, 2020

Thanks It is working.

@jogaec22
Copy link

jogaec22 commented Oct 14, 2020

Thanks, it helped me!

@DmMhs
Copy link

DmMhs commented Oct 15, 2020

@chrisivens you've saved my day. Thanks

@murtajaziad
Copy link

murtajaziad commented Oct 16, 2020

Thanks a lot 🌹

@devinrhode2
Copy link

devinrhode2 commented Nov 5, 2020

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

[alias]
  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

@aramodi
Copy link

aramodi commented Nov 21, 2020

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

@sangeethkumarn
Copy link

sangeethkumarn commented Feb 18, 2021

Thank you so much

@Mohitmadhav
Copy link

Mohitmadhav commented May 12, 2021

Thanks a lot !! Worked smoothly

@SynCap
Copy link

SynCap commented Jul 28, 2021

PowerShell function/module for this https://gist.github.com/30b513e40ada3e171e99e57d25c45f1c

@vishalbandre
Copy link

vishalbandre commented Oct 5, 2021

Thanks, great. It works as expected.

@Rakesh-space
Copy link

Rakesh-space commented Oct 28, 2021

Thanks you

@bidehalaxmi
Copy link

bidehalaxmi commented Jan 12, 2022

Thank you

@sunnycs121
Copy link

sunnycs121 commented Jan 31, 2022

Very helpful. Thank you so much!

@adedoyin-paystack
Copy link

adedoyin-paystack commented Mar 22, 2022

Thank you very much

@HaSS-1
Copy link

HaSS-1 commented Mar 29, 2022

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

@Al-Mutawakkil
Copy link

Al-Mutawakkil commented Apr 11, 2022

Thank You so much!

@au-kirkwang
Copy link

au-kirkwang commented Sep 8, 2022

Very helpful. Thank you so much!

@lorenzogrv
Copy link

lorenzogrv commented Sep 14, 2022

Thanks! (again) ❤️

@Rayhan-khan-Ridoy
Copy link

Rayhan-khan-Ridoy commented Nov 3, 2022

thanks...its working

@deSp44
Copy link

deSp44 commented Nov 30, 2022

Thanks!

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