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
@jfavrod

This comment has been minimized.

Copy link

@jfavrod jfavrod commented Dec 15, 2014

Mahalo.

@AnneTheAgile

This comment has been minimized.

Copy link

@AnneTheAgile AnneTheAgile commented Dec 16, 2014

On the first step, it also works to rename the currently checked out branch, per https://www.atlassian.com/git/tutorials/using-branches/git-branch.

git branch -m new_branch
@idrummer83

This comment has been minimized.

Copy link

@idrummer83 idrummer83 commented Jan 30, 2015

cool!!!!!!!!!!!!! thank's a lot

@antarr

This comment has been minimized.

Copy link

@antarr antarr commented Mar 31, 2015

πŸ‘ πŸ‘ πŸ‘ πŸ‘ πŸ‘

@FezVrasta

This comment has been minimized.

Copy link

@FezVrasta FezVrasta commented Apr 26, 2015

Remember to pull before! Or you may lose every commit not yet on your local branch

@chrisivens

This comment has been minimized.

Copy link

@chrisivens chrisivens commented May 5, 2015

For some reason my version of git (v2.3.2 on osx) keeps reusing the same remote branch name whether using --set-upstream or -u.
You could of course edit the gitconfig file but if this is happening to you, try using git branch --unset-upstream before doing the final push.

@oduserdnov

This comment has been minimized.

Copy link

@oduserdnov oduserdnov commented Jun 22, 2015

Nice one, ty!

@donfakk

This comment has been minimized.

Copy link

@donfakk donfakk commented Jun 23, 2015

Good tip!

@joobei

This comment has been minimized.

Copy link

@joobei joobei commented Jun 26, 2015

Thank you!!

@elfen

This comment has been minimized.

Copy link

@elfen elfen commented Jun 26, 2015

Thank you!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 6, 2015

Thank you!

@perguth

This comment has been minimized.

Copy link

@perguth perguth commented Jul 7, 2015

πŸ˜„ Thanks, mate!

@rrolla

This comment has been minimized.

Copy link

@rrolla rrolla commented Jul 10, 2015

Thanks.

@biniama

This comment has been minimized.

Copy link

@biniama biniama commented Jul 14, 2015

+100

@yiweig

This comment has been minimized.

Copy link

@yiweig yiweig commented Jul 26, 2015

+1

@sebastian-code

This comment has been minimized.

Copy link

@sebastian-code sebastian-code commented Jul 28, 2015

Of great help, thanks a lot.

@danmcadams

This comment has been minimized.

Copy link

@danmcadams danmcadams commented Jul 28, 2015

Perfect!

@PerpetualBeta

This comment has been minimized.

Copy link

@PerpetualBeta PerpetualBeta commented Aug 6, 2015

Much appreciated.

@mlbright

This comment has been minimized.

Copy link

@mlbright mlbright commented Aug 19, 2015

+1

@johnheix-g

This comment has been minimized.

Copy link

@johnheix-g johnheix-g commented Aug 23, 2015

Great!

@ryabrody

This comment has been minimized.

Copy link

@ryabrody ryabrody commented Aug 25, 2015

Thanks but is there a way to rename a branch remotely without deleting it? As example if you are having an open Pull Request?

@piemonkey

This comment has been minimized.

Copy link

@piemonkey piemonkey commented Aug 28, 2015

I don't think there's any way to do a rename. I'm guessing this is because in plain git there's no difference between renaming and deleting then creating a new branch. It's only with pull-requests that you start to have problems, so Github would need to build the feature themselves.

If you've accidentally made a pull request from your master and want to keep using it as a master branch, you can always make a new branch, change the default in the repository settings, then use that one.

@spencer1573

This comment has been minimized.

Copy link

@spencer1573 spencer1573 commented Sep 9, 2015

to the author: its people like you that make the internet great!

@itsdarrylnorris

This comment has been minimized.

Copy link

@itsdarrylnorris itsdarrylnorris commented Sep 11, 2015

1+ this works like charm.

@mks-d

This comment has been minimized.

Copy link

@mks-d mks-d commented Sep 14, 2015

+1

@igor-brishkoski

This comment has been minimized.

Copy link

@igor-brishkoski igor-brishkoski commented Sep 18, 2015

Noiceeee

@Juszczak

This comment has been minimized.

Copy link

@Juszczak Juszczak commented Sep 21, 2015

+1

@jibusayone

This comment has been minimized.

Copy link

@jibusayone jibusayone commented Sep 23, 2015

Thanks !

@DieterReuter

This comment has been minimized.

Copy link

@DieterReuter DieterReuter commented Sep 23, 2015

Thanks, you made my day!

@CelticParser

This comment has been minimized.

Copy link

@CelticParser CelticParser commented Sep 24, 2015

Awesome!!

@reachsrirams

This comment has been minimized.

Copy link

@reachsrirams reachsrirams commented Sep 29, 2015

Thanks a lot.

@ve3

This comment has been minimized.

Copy link

@ve3 ve3 commented Oct 12, 2015

It's working! But for me, I need to run as @chrisivens said.

@newshorts

This comment has been minimized.

Copy link

@newshorts newshorts commented Oct 15, 2015

thanks for this!

@rusikf

This comment has been minimized.

Copy link

@rusikf rusikf commented Oct 22, 2015

Awesome, thanks!

@terales

This comment has been minimized.

Copy link

@terales terales commented Oct 31, 2015

Thanks a lot!

@mustafaerturk

This comment has been minimized.

Copy link

@mustafaerturk mustafaerturk commented Nov 4, 2015

works like a nah !!!

@markmevans

This comment has been minimized.

Copy link

@markmevans markmevans commented Nov 21, 2015

The original git push origin :old_branch syntax doesn't work for me on OS X with a recent git (version 2.6.2). For anyone running into this, the new syntax is:

git push origin --delete old_branch
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 25, 2015

+1

@dashmug

This comment has been minimized.

Copy link

@dashmug dashmug commented Nov 25, 2015

@markmevans, it worked on my El Capitan with git 2.6.3. The old syntax that is.

@ryanpcmcquen

This comment has been minimized.

Copy link

@ryanpcmcquen ryanpcmcquen commented Dec 3, 2015

@markmevans, also works on Slackware with git 2.6.3.

@VasiRahman

This comment has been minimized.

Copy link

@VasiRahman VasiRahman commented Dec 7, 2015

thanks

@dfons

This comment has been minimized.

Copy link

@dfons dfons commented Dec 10, 2015

Great!

@ignlg

This comment has been minimized.

Copy link

@ignlg ignlg commented Dec 15, 2015

@markmevans the :old_branch syntax worked perfectly on Mavericks with git 2.6.3

@pere5

This comment has been minimized.

Copy link

@pere5 pere5 commented Dec 17, 2015

This is absolutely incredible!

@lthai423

This comment has been minimized.

Copy link

@lthai423 lthai423 commented Jan 16, 2016

+1

@mthadisena

This comment has been minimized.

Copy link

@mthadisena mthadisena commented Jan 16, 2016

Thank you so much!

@pdp10

This comment has been minimized.

Copy link

@pdp10 pdp10 commented Jan 27, 2016

Many thanks, very useful.

@raincrash

This comment has been minimized.

Copy link

@raincrash raincrash commented Jan 27, 2016

Thanks!

@joshlbaker

This comment has been minimized.

Copy link

@joshlbaker joshlbaker commented Jan 27, 2016

Thanks! Worked like a charm

@tetreis

This comment has been minimized.

Copy link

@tetreis tetreis commented Feb 2, 2016

Thanks!

@michalbiarda

This comment has been minimized.

Copy link

@michalbiarda michalbiarda commented Feb 3, 2016

Thanks.

@rcconsult

This comment has been minimized.

Copy link

@rcconsult rcconsult commented Feb 3, 2016

+1

@KKirad

This comment has been minimized.

Copy link

@KKirad KKirad commented Feb 4, 2016

Thanks..!!

@DoubleDi

This comment has been minimized.

Copy link

@DoubleDi DoubleDi commented Feb 16, 2016

Thanks so much!

@nav-mike

This comment has been minimized.

Copy link

@nav-mike nav-mike commented Feb 24, 2016

Thanks!

@carolynfischer

This comment has been minimized.

Copy link

@carolynfischer carolynfischer commented Feb 29, 2016

Thank you!

@mshivanna

This comment has been minimized.

Copy link

@mshivanna mshivanna commented Mar 5, 2016

+1

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 5, 2016

πŸ‘

@DaveLindberg

This comment has been minimized.

Copy link

@DaveLindberg DaveLindberg commented Mar 17, 2016

:-)

@gpashis

This comment has been minimized.

Copy link

@gpashis gpashis commented Mar 21, 2016

Thanks. Works like a charm.

@seoyoochan

This comment has been minimized.

Copy link

@seoyoochan seoyoochan commented Mar 25, 2016

πŸ‘

@KITSTuhinChandra

This comment has been minimized.

Copy link

@KITSTuhinChandra KITSTuhinChandra commented Mar 30, 2016

"git branch -m new_branch" --> this one worked. But both the branches will actually exist in git. I think this command just creates a copy of the branch with new name.

@poisvin

This comment has been minimized.

Copy link

@poisvin poisvin commented Apr 14, 2016

πŸ‘ thank you

@ShuaiyiWu

This comment has been minimized.

Copy link

@ShuaiyiWu ShuaiyiWu commented Apr 27, 2016

Thanks

@ve3

This comment has been minimized.

Copy link

@ve3 ve3 commented Apr 27, 2016

git push origin :oldbranch

! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to ....

@astromahi

This comment has been minimized.

Copy link

@astromahi astromahi commented May 2, 2016

Can we do like this?.
git push -u origin :oldbranch newbranch

@greuze

This comment has been minimized.

Copy link

@greuze greuze commented May 5, 2016

Cool, worked like a charm πŸ‘

@Vrakfall

This comment has been minimized.

Copy link

@Vrakfall Vrakfall commented May 11, 2016

I actually would remove the remote branch after pushing the new local one in order to make sure we don't lose the data. If the disk drive gets corrupted between the current command 2 and 3, the data is lost. By inverting them, that case is prevented.

@mitendraanand

This comment has been minimized.

Copy link

@mitendraanand mitendraanand commented May 18, 2016

## If you are using SourceTree.

  1. Rename locally. Right click on local branch and there is rename option.
  2. Do some code changes.
  3. Commit, push to remote.
  4. You will see your new branch with the renamed name(old one exists too, you can delete if you want).
@axedre

This comment has been minimized.

Copy link

@axedre axedre commented May 25, 2016

Brilliant, thanks! πŸ‘

@tbhockey

This comment has been minimized.

Copy link

@tbhockey tbhockey commented May 25, 2016

Excellent. thanks.

@stefanwimmer128

This comment has been minimized.

Copy link

@stefanwimmer128 stefanwimmer128 commented Jun 13, 2016

@astromahi You cant delete master. You have to set a new default branch in the project settings first.

@yen3

This comment has been minimized.

Copy link

@yen3 yen3 commented Jun 14, 2016

It's very useful. Thank you.

@PixelAtLarge

This comment has been minimized.

Copy link

@PixelAtLarge PixelAtLarge commented Jun 17, 2016

Thank you!

@PedramMarandi

This comment has been minimized.

Copy link

@PedramMarandi PedramMarandi commented Jun 29, 2016

Thank you

@gdibble

This comment has been minimized.

Copy link

@gdibble gdibble commented Jun 30, 2016

πŸ‘

@rifansi

This comment has been minimized.

Copy link

@rifansi rifansi commented Jul 4, 2016

thank you

@crmpicco

This comment has been minimized.

Copy link

@crmpicco crmpicco commented Jul 8, 2016

πŸ‘

@iamajvillalobos

This comment has been minimized.

Copy link

@iamajvillalobos iamajvillalobos commented Jul 11, 2016

πŸ‘

@deronimo

This comment has been minimized.

Copy link

@deronimo deronimo commented Jul 13, 2016

πŸ‘

@chintan9

This comment has been minimized.

Copy link

@chintan9 chintan9 commented Jul 29, 2016

thanks

@LeeRuns

This comment has been minimized.

Copy link

@LeeRuns LeeRuns commented Aug 10, 2016

dope

@Sanyambansal76

This comment has been minimized.

Copy link

@Sanyambansal76 Sanyambansal76 commented Aug 11, 2016

cool...thanks

@unlessbamboo

This comment has been minimized.

Copy link

@unlessbamboo unlessbamboo commented Aug 22, 2016

Thanks.

@Abdelalim-dev

This comment has been minimized.

Copy link

@Abdelalim-dev Abdelalim-dev commented Aug 22, 2016

+1

@Mantoudad

This comment has been minimized.

Copy link

@Mantoudad Mantoudad commented Aug 23, 2016

Thanks!

@baurzhan-konurbayev

This comment has been minimized.

Copy link

@baurzhan-konurbayev baurzhan-konurbayev commented Aug 26, 2016

Nice, thanks!

@marcuslang

This comment has been minimized.

Copy link

@marcuslang marcuslang commented Aug 29, 2016

+1

@lexigen

This comment has been minimized.

Copy link

@lexigen lexigen commented Aug 30, 2016

thank you!

@TamarAmit

This comment has been minimized.

Copy link

@TamarAmit TamarAmit commented Sep 7, 2016

Thanks! :)

@sudhap

This comment has been minimized.

Copy link

@sudhap sudhap commented Sep 12, 2016

It worked, Thanks a lot

@jm-janzen

This comment has been minimized.

Copy link

@jm-janzen jm-janzen commented Sep 25, 2016

Can't believe it was so simple. Thanks!

@erlinis

This comment has been minimized.

Copy link

@erlinis erlinis commented Sep 28, 2016

Thanks ❀️

@sheikamp

This comment has been minimized.

Copy link

@sheikamp sheikamp commented Oct 6, 2016

Great, thanks.

@ManuelPe

This comment has been minimized.

Copy link

@ManuelPe ManuelPe commented Oct 6, 2016

Is recommended that if you have a pull request wait to rename, until is accepted.

@ekta-slit

This comment has been minimized.

Copy link

@ekta-slit ekta-slit commented Oct 13, 2016

πŸ‘

@jaymedavis

This comment has been minimized.

Copy link

@jaymedavis jaymedavis commented Oct 14, 2016

I start branch names with the PR number I am addressing (keep things organized and readying myself for waffle bot). In this case it was #520-application-fee-refunds. I kept getting an error of fatal: branch name required when executing
git branch -m #520-application-fee-refunds #509-application-fee-refunds

Simple fix was to wrap quotes around the branch names
git branch -m "#520-application-fee-refunds" "#509-application-fee-refunds"

Also, when you are pushing the branch, you will need the quotes too:
git push --set-upstream origin "#509-application-fee-refunds"

Perhaps common sense for some, but hope it can help someone. πŸ‘

@zanderwar

This comment has been minimized.

Copy link

@zanderwar zanderwar commented Oct 17, 2016

πŸ‘

@PeterRJones

This comment has been minimized.

Copy link

@PeterRJones PeterRJones commented Nov 9, 2016

πŸ‘ Just what I needed.

@AdrianAntunez

This comment has been minimized.

Copy link

@AdrianAntunez AdrianAntunez commented Nov 10, 2016

Simple and clear. Thank you so much! ;)

@enockoloo6

This comment has been minimized.

Copy link

@enockoloo6 enockoloo6 commented Nov 18, 2016

great staff man!!

@jvanasco

This comment has been minimized.

Copy link

@jvanasco jvanasco commented Nov 24, 2016

You should reverse the last two steps and do:

  • write new
  • delete old

otherwise you end up with potentially losing all your work between the delete and write:

  • delete old

  • <what if computer crashes or git crashes?>

  • write new

    git branch -m old_branch new_branch # Rename branch locally
    git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
    git push origin :old_branch # Delete the old branch

@giacomolm

This comment has been minimized.

Copy link

@giacomolm giacomolm commented Dec 5, 2016

It's also useful to use rebase, and use the reword message to change commit message https://help.github.com/articles/changing-a-commit-message/

@parvezk

This comment has been minimized.

Copy link

@parvezk parvezk commented Dec 5, 2016

Thank you very much. What if there is an open PR in the old branch. would that point to the new branch or get deleted along the old branch?

@sampatbadhe

This comment has been minimized.

Copy link

@sampatbadhe sampatbadhe commented Dec 8, 2016

πŸ‘

@samplestudio

This comment has been minimized.

Copy link

@samplestudio samplestudio commented Dec 15, 2016

it's not rename, it's delete and create new

@PolinaKhainson

This comment has been minimized.

Copy link

@PolinaKhainson PolinaKhainson commented Dec 16, 2016

Thaaaanks!!! The most useful link for today))

@kelseyll

This comment has been minimized.

Copy link

@kelseyll kelseyll commented Dec 16, 2016

πŸ‘

@sarkiroka

This comment has been minimized.

Copy link

@sarkiroka sarkiroka commented Dec 17, 2016

+1.41

@sonk02

This comment has been minimized.

Copy link

@sonk02 sonk02 commented Dec 21, 2016

Great..It worked for me

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 21, 2016

Thank you very much πŸ’™

@antonkomarev

This comment has been minimized.

Copy link

@antonkomarev antonkomarev commented Dec 23, 2016

One of the most useful tricks.

@cassiano0

This comment has been minimized.

Copy link

@cassiano0 cassiano0 commented Jan 8, 2017

Thank you!

@vmnikhil

This comment has been minimized.

Copy link

@vmnikhil vmnikhil commented Jan 16, 2017

πŸ‘

@mrakitin

This comment has been minimized.

Copy link

@mrakitin mrakitin commented Jan 25, 2017

Thanks!

@krthkj

This comment has been minimized.

Copy link

@krthkj krthkj commented Jan 26, 2017

Thanks πŸ‘

@twhoward99

This comment has been minimized.

Copy link

@twhoward99 twhoward99 commented Jan 30, 2017

+1

@RuanHerculano

This comment has been minimized.

Copy link

@RuanHerculano RuanHerculano commented Feb 6, 2017

thanks

@shanebo

This comment has been minimized.

Copy link

@shanebo shanebo commented Feb 8, 2017

+1

@rjesususa

This comment has been minimized.

Copy link

@rjesususa rjesususa commented Feb 10, 2017

thanks a lot!

@constanzekratel

This comment has been minimized.

Copy link

@constanzekratel constanzekratel commented Feb 27, 2017

Thanks πŸ‘ !

@Wing333

This comment has been minimized.

Copy link

@Wing333 Wing333 commented Mar 6, 2017

Simple yet useful! Thanks a lot!

@ChristopherMortensen

This comment has been minimized.

Copy link

@ChristopherMortensen ChristopherMortensen commented Mar 8, 2017

Thanks!

@luksrn

This comment has been minimized.

Copy link

@luksrn luksrn commented Mar 15, 2017

+1

@jayeshbadgujar

This comment has been minimized.

Copy link

@jayeshbadgujar jayeshbadgujar commented Mar 17, 2017

Thanks.

@thatflow

This comment has been minimized.

Copy link

@thatflow thatflow commented Mar 20, 2017

Goog job !

@v0d0m3r

This comment has been minimized.

Copy link

@v0d0m3r v0d0m3r commented Mar 24, 2017

Thank you!

@rymacivorkuy

This comment has been minimized.

Copy link

@rymacivorkuy rymacivorkuy commented Apr 6, 2017

Thank you kind person.... kind soul. Accurate information is where it's at. So much better than that inaccurate information junk.

@gracenellore

This comment has been minimized.

Copy link

@gracenellore gracenellore commented Apr 9, 2017

Thank you! πŸ‘

@masini4ka

This comment has been minimized.

Copy link

@masini4ka masini4ka commented Apr 30, 2017

Thanks!!! Saved me time πŸ‘

@TkachenkoAA

This comment has been minimized.

Copy link

@TkachenkoAA TkachenkoAA commented May 5, 2017

Thanks!!! Saved me lifeπŸ‘

@ssonlucera

This comment has been minimized.

Copy link

@ssonlucera ssonlucera commented May 17, 2017

Thank you! πŸ‘

@aliuosio

This comment has been minimized.

Copy link

@aliuosio aliuosio commented Jun 2, 2017

Say yeah

@Teshlen

This comment has been minimized.

Copy link

@Teshlen Teshlen commented Jun 12, 2017

Thank you!

@Landmaster

This comment has been minimized.

Copy link

@Landmaster Landmaster commented Jun 17, 2017

Thanks!

@wakedeer

This comment has been minimized.

Copy link

@wakedeer wakedeer commented Jun 22, 2017

thanks a lot!!!!!!!

@abhishekthorat-tudip

This comment has been minimized.

Copy link

@abhishekthorat-tudip abhishekthorat-tudip commented Jul 1, 2017

Thanks 1+

@jpkempf

This comment has been minimized.

Copy link

@jpkempf jpkempf commented Jul 5, 2017

Google patiently keeps leading me back here, thank you! :D

@jbrant

This comment has been minimized.

Copy link

@jbrant jbrant commented Jul 5, 2017

This doesn't entirely work; it renames locally but maintains a mapping to the old github branch name such that it still recreates the branch on github with the old name.

@lmurta05

This comment has been minimized.

Copy link

@lmurta05 lmurta05 commented Jul 12, 2017

@lttlrck please update your gist. If the upstream is still set, it will be set to track again the remote with the original name. git version 2.11 on windows if it's relevant

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch
git branch --unset-upstream new_branch      # Unset the tracking info, so it doesn't push with the old name
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote
@ngyunter

This comment has been minimized.

Copy link

@ngyunter ngyunter commented Jul 13, 2017

@lmurta05 Working as described. Thank you very much. πŸ‘

@jilucev

This comment has been minimized.

Copy link

@jilucev jilucev commented Jul 14, 2017

Such an excellent and concise solution. I've googled for this and come back to it more than ten times now. Thank you.

@umar-zahoor

This comment has been minimized.

Copy link

@umar-zahoor umar-zahoor commented Jul 15, 2017

Thanks +1

@David-II

This comment has been minimized.

Copy link

@David-II David-II commented Aug 17, 2017

It works, thank you!

@mcanalesmayo

This comment has been minimized.

Copy link

@mcanalesmayo mcanalesmayo commented Aug 28, 2017

@lttlrck be careful. First of all make sure the new branch is created, then delete the old branch. This will ensure everything is fine even if something crashes between creating the new branch and deleting the old one.

git branch -m old_branch new_branch
git push --set-upstream origin new_branch
git push origin :old_branch
@maksim77

This comment has been minimized.

Copy link

@maksim77 maksim77 commented Sep 6, 2017

Thx!

@karthickkumar00

This comment has been minimized.

Copy link

@karthickkumar00 karthickkumar00 commented Oct 21, 2017

It works.Thanks!

@tigusigalpa

This comment has been minimized.

Copy link

@tigusigalpa tigusigalpa commented Oct 25, 2017

@lmurta05 Thanks a lot!

@SikindarMirza

This comment has been minimized.

Copy link

@SikindarMirza SikindarMirza commented Oct 31, 2017

Thanks a lot

@liuliqiang

This comment has been minimized.

Copy link

@liuliqiang liuliqiang commented Nov 9, 2017

πŸ‘ Thank you guy...

@squoddam

This comment has been minimized.

Copy link

@squoddam squoddam commented Nov 9, 2017

Awesome script!

@kevinxh

This comment has been minimized.

Copy link

@kevinxh kevinxh commented Nov 20, 2017

πŸ‘

@imdzeeshan

This comment has been minimized.

Copy link

@imdzeeshan imdzeeshan commented Nov 29, 2017

@MarcosCM πŸ‘

@XxXtraCookie

This comment has been minimized.

Copy link

@XxXtraCookie XxXtraCookie commented Dec 8, 2017

wow, thanks a lot! Saved my life ;p

@anaray

This comment has been minimized.

Copy link

@anaray anaray commented Dec 13, 2017

Thanks a lot ! This is clean.

@auraz

This comment has been minimized.

Copy link

@auraz auraz commented Dec 14, 2017

Thanks!

@bitbonk

This comment has been minimized.

Copy link

@bitbonk bitbonk commented Dec 19, 2017

I think you should add git checkout old_branch as the first step to make sure the local tracking branch actually exists. If you don't do it you may get the following error:

> git branch -m myoldbranch mynewbranch
error: refname myoldbranch not found
fatal: Branch rename failed
@shuaiming

This comment has been minimized.

Copy link

@shuaiming shuaiming commented Jan 11, 2018

Thanks a billion!

@alannaidon

This comment has been minimized.

Copy link

@alannaidon alannaidon commented Jan 16, 2018

Thank you !!

@CKGrafico

This comment has been minimized.

Copy link

@CKGrafico CKGrafico commented Jan 16, 2018

Thanks!

@murcix

This comment has been minimized.

Copy link

@murcix murcix commented Jan 29, 2018

thankssssssss

@cjleth

This comment has been minimized.

Copy link

@cjleth cjleth commented Feb 8, 2018

tysm

@umairaslamm

This comment has been minimized.

Copy link

@umairaslamm umairaslamm commented Feb 9, 2018

thanks a lot πŸ’―

@SeunghunSunmoonLee

This comment has been minimized.

Copy link

@SeunghunSunmoonLee SeunghunSunmoonLee commented Feb 15, 2018

thank you!

@shopglobal

This comment has been minimized.

Copy link

@shopglobal shopglobal commented Feb 17, 2018

Still good! +1

@cozzin

This comment has been minimized.

Copy link

@cozzin cozzin commented Mar 8, 2018

thank you!

@PunniyaPrabhu

This comment has been minimized.

Copy link

@PunniyaPrabhu PunniyaPrabhu commented Mar 12, 2018

Thank u

@ygnoh

This comment has been minimized.

Copy link

@ygnoh ygnoh commented Mar 15, 2018

thank you

@hezymal

This comment has been minimized.

Copy link

@hezymal hezymal commented Mar 15, 2018

Thank You!

@hugo-marcelo

This comment has been minimized.

Copy link

@hugo-marcelo hugo-marcelo commented Mar 15, 2018

thanks man!

@TorvaldsDB

This comment has been minimized.

Copy link

@TorvaldsDB TorvaldsDB commented Mar 19, 2018

Delete the old branch
git push origin --delete :old_branch

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 29, 2018

@TorvaldsDB good catch

@ronan-ganachaud-cbp

This comment has been minimized.

Copy link

@ronan-ganachaud-cbp ronan-ganachaud-cbp commented Apr 4, 2018

You can combine the push operations :
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch new_branch # Delete the old branch and push the new one

@alphaCTzo7G

This comment has been minimized.

Copy link

@alphaCTzo7G alphaCTzo7G commented Apr 11, 2018

This will only work if the :oldbranch is not the active branch of the remote repo. If it is, and you have write access to the repo (such as through a server), you can do git symbolic-ref HEAD refs/heads/newbranch, and then delete the repo: http://feeding.cloud.geek.nz/2008/12/setting-default-git-branch-in-bare.html

If the remote branch is a Github repo, you wont be able to do this. So you will have to change it through the Github interface: https://stackoverflow.com/a/2962737/4752883

@ffoodd

This comment has been minimized.

Copy link

@ffoodd ffoodd commented Apr 26, 2018

FWIW you can use an alias to make it simpler. In .gitconfig:

[alias]
	rename = "!moveit() { git branch -m $1 $2; git push origin :$1; git push --set-upstream origin $2; }; moveit"

Then simply run git rename old_branch new_branch, and it's done.

@mbraunerDE

This comment has been minimized.

Copy link

@mbraunerDE mbraunerDE commented Apr 27, 2018

Thank you!

@adamteller-macmillan

This comment has been minimized.

Copy link

@adamteller-macmillan adamteller-macmillan commented May 1, 2018

Thank you. This worked perfectly. I ran the 3 steps at top of this gist.

@mshRoR

This comment has been minimized.

Copy link

@mshRoR mshRoR commented May 11, 2018

Rename a local and remote branch in git this article describe details about git branch rename locally and remotely

@YehualashetGit

This comment has been minimized.

Copy link

@YehualashetGit YehualashetGit commented May 17, 2018

Thank a lot!!

@olekhy

This comment has been minimized.

Copy link

@olekhy olekhy commented Jun 7, 2018

we @ALL are crank here

@japutko

This comment has been minimized.

Copy link

@japutko japutko commented Jun 20, 2018

thanks man!

@bherzig-nist

This comment has been minimized.

Copy link

@bherzig-nist bherzig-nist commented Jun 20, 2018

Cheers! πŸ‘

@rafaelsm

This comment has been minimized.

Copy link

@rafaelsm rafaelsm commented Jun 26, 2018

Thanks!

@lomse

This comment has been minimized.

Copy link

@lomse lomse commented Jun 28, 2018

Very helpful

@nathanyoung

This comment has been minimized.

Copy link

@nathanyoung nathanyoung commented Jul 6, 2018

Thank you!

@dr-smyslov

This comment has been minimized.

Copy link

@dr-smyslov dr-smyslov commented Jul 13, 2018

thx!

@proton5000

This comment has been minimized.

Copy link

@proton5000 proton5000 commented Jul 17, 2018

+1

@AsharDweedar

This comment has been minimized.

Copy link

@AsharDweedar AsharDweedar commented Jul 22, 2018

+1

@hafeez84

This comment has been minimized.

Copy link

@hafeez84 hafeez84 commented Aug 3, 2018

thanks alot πŸ‘

@manueldeveloper

This comment has been minimized.

Copy link

@manueldeveloper manueldeveloper commented Aug 15, 2018

πŸ‘

@qrobin

This comment has been minimized.

Copy link

@qrobin qrobin commented Sep 19, 2018

Awesome, thank you =)

@hanswimtj

This comment has been minimized.

Copy link

@hanswimtj hanswimtj commented Sep 27, 2018

+1

@DmitriyYukhanov

This comment has been minimized.

Copy link

@DmitriyYukhanov DmitriyYukhanov commented Oct 3, 2018

πŸ”₯

@webuxr

This comment has been minimized.

Copy link

@webuxr webuxr commented Oct 24, 2018

Nice!
I especially like @ffoodd 's alias.
πŸ‘

@ranjan-2209

This comment has been minimized.

Copy link

@ranjan-2209 ranjan-2209 commented Oct 26, 2018

thanks a lot

@ayatmaulana

This comment has been minimized.

Copy link

@ayatmaulana ayatmaulana commented Nov 22, 2018

awesome, very helpful

@gjc9620

This comment has been minimized.

Copy link

@gjc9620 gjc9620 commented