Skip to content

Instantly share code, notes, and snippets.

@lttlrck
Created March 18, 2014 20:34
Show Gist options
  • Save lttlrck/9628955 to your computer and use it in GitHub Desktop.
Save lttlrck/9628955 to your computer and use it in GitHub Desktop.
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
@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

Thank you so much

@Mohitmadhav
Copy link

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

Thanks, great. It works as expected.

@Rakesh-space
Copy link

Thanks you

@bidehalaxmi
Copy link

Thank you

@sunnycs121
Copy link

Very helpful. Thank you so much!

@adedoyin-paystack
Copy link

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

Thank You so much!

@au-kirkwang
Copy link

Very helpful. Thank you so much!

@lorenzogrv
Copy link

Thanks! (again) ❤️

@Rayhan-khan-Ridoy
Copy link

thanks...its working

@deSp44
Copy link

deSp44 commented Nov 30, 2022

Thanks!

@Kaleyemijowurolo
Copy link

awesome

@codewithmanas
Copy link

Thanks!

@AdarshGaur01
Copy link

Thanks. It worked like a charm.

@jriegner
Copy link

Thanks!

@Alan052918
Copy link

Thanks!

@renehu-gd
Copy link

Great!

@JohannSuarez
Copy link

Still works, thanks a lot!

@hulopza
Copy link

hulopza commented Oct 3, 2023

Helped a bunch, thanks!

@kal40
Copy link

kal40 commented Oct 4, 2023

Thanks!

@mmhg-hamed
Copy link

Perfect, thanks

@lissethamc
Copy link

Thank you! It worked, It only made me do my last PR again but not a big deal

@timuretis
Copy link

👑

@GilShalit
Copy link

This can be done via the GitHub site

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