Create a gist now

Instantly share code, notes, and snippets.

Embed
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
@not--p

This comment has been minimized.

Show comment
Hide comment

not--p commented Dec 15, 2014

Mahalo.

@AnneTheAgile

This comment has been minimized.

Show comment
Hide comment
@AnneTheAgile

AnneTheAgile 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

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.

Show comment
Hide comment
@idrummer83

idrummer83 Jan 30, 2015

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

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

@antarr

This comment has been minimized.

Show comment
Hide comment
@antarr

antarr Mar 31, 2015

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

antarr commented Mar 31, 2015

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

@FezVrasta

This comment has been minimized.

Show comment
Hide comment
@FezVrasta

FezVrasta Apr 26, 2015

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

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

@chrisivens

This comment has been minimized.

Show comment
Hide comment
@chrisivens

chrisivens 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.

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.

Show comment
Hide comment
@oduserdnov

oduserdnov Jun 22, 2015

Nice one, ty!

Nice one, ty!

@donfak

This comment has been minimized.

Show comment
Hide comment
@donfak

donfak Jun 23, 2015

Good tip!

donfak commented Jun 23, 2015

Good tip!

@joobei

This comment has been minimized.

Show comment
Hide comment
@joobei

joobei Jun 26, 2015

Thank you!!

joobei commented Jun 26, 2015

Thank you!!

@elfen

This comment has been minimized.

Show comment
Hide comment
@elfen

elfen Jun 26, 2015

Thank you!

elfen commented Jun 26, 2015

Thank you!

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jul 6, 2015

Thank you!

ghost commented Jul 6, 2015

Thank you!

@perguth

This comment has been minimized.

Show comment
Hide comment
@perguth

perguth Jul 7, 2015

πŸ˜„ Thanks, mate!

perguth commented Jul 7, 2015

πŸ˜„ Thanks, mate!

@djbeater

This comment has been minimized.

Show comment
Hide comment

Thanks.

@biniama

This comment has been minimized.

Show comment
Hide comment

biniama commented Jul 14, 2015

+100

@yiweig

This comment has been minimized.

Show comment
Hide comment

yiweig commented Jul 26, 2015

+1

@sebastian-code

This comment has been minimized.

Show comment
Hide comment
@sebastian-code

sebastian-code Jul 28, 2015

Of great help, thanks a lot.

Of great help, thanks a lot.

@danmcadams

This comment has been minimized.

Show comment
Hide comment

Perfect!

@PerpetualBeta

This comment has been minimized.

Show comment
Hide comment
@PerpetualBeta

PerpetualBeta Aug 6, 2015

Much appreciated.

Much appreciated.

@mlbright

This comment has been minimized.

Show comment
Hide comment

+1

@johnheix-g

This comment has been minimized.

Show comment
Hide comment

Great!

@ryabrody

This comment has been minimized.

Show comment
Hide comment
@ryabrody

ryabrody 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?

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.

Show comment
Hide comment
@piemonkey

piemonkey 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.

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.

Show comment
Hide comment
@spencer1573

spencer1573 Sep 9, 2015

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

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

@darol100

This comment has been minimized.

Show comment
Hide comment
@darol100

darol100 Sep 11, 2015

1+ this works like charm.

1+ this works like charm.

@mks-d

This comment has been minimized.

Show comment
Hide comment

mks-d commented Sep 14, 2015

+1

@igor-brishkoski

This comment has been minimized.

Show comment
Hide comment

Noiceeee

@Juszczak

This comment has been minimized.

Show comment
Hide comment

+1

@jibusayone

This comment has been minimized.

Show comment
Hide comment

Thanks !

@DieterReuter

This comment has been minimized.

Show comment
Hide comment
@DieterReuter

DieterReuter Sep 23, 2015

Thanks, you made my day!

Thanks, you made my day!

@CelticParser

This comment has been minimized.

Show comment
Hide comment

Awesome!!

@reachsrirams

This comment has been minimized.

Show comment
Hide comment
@reachsrirams

reachsrirams Sep 29, 2015

Thanks a lot.

Thanks a lot.

@ve3

This comment has been minimized.

Show comment
Hide comment
@ve3

ve3 Oct 12, 2015

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

ve3 commented Oct 12, 2015

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

@newshorts

This comment has been minimized.

Show comment
Hide comment
@newshorts

newshorts Oct 15, 2015

thanks for this!

thanks for this!

@rusikf

This comment has been minimized.

Show comment
Hide comment
@rusikf

rusikf Oct 22, 2015

Awesome, thanks!

rusikf commented Oct 22, 2015

Awesome, thanks!

@terales

This comment has been minimized.

Show comment
Hide comment
@terales

terales Oct 31, 2015

Thanks a lot!

terales commented Oct 31, 2015

Thanks a lot!

@mustafaerturk

This comment has been minimized.

Show comment
Hide comment
@mustafaerturk

mustafaerturk Nov 4, 2015

works like a nah !!!

works like a nah !!!

@markmevans

This comment has been minimized.

Show comment
Hide comment
@markmevans

markmevans 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

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.

Show comment
Hide comment

ghost commented Nov 25, 2015

+1

@dashmug

This comment has been minimized.

Show comment
Hide comment
@dashmug

dashmug Nov 25, 2015

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

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.

Show comment
Hide comment
@ryanpcmcquen

ryanpcmcquen Dec 3, 2015

@markmevans, also works on Slackware with git 2.6.3.

@markmevans, also works on Slackware with git 2.6.3.

@VasiRahman

This comment has been minimized.

Show comment
Hide comment

thanks

@dfons

This comment has been minimized.

Show comment
Hide comment

dfons commented Dec 10, 2015

Great!

@ignlg

This comment has been minimized.

Show comment
Hide comment
@ignlg

ignlg Dec 15, 2015

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

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.

Show comment
Hide comment
@pere5

pere5 Dec 17, 2015

This is absolutely incredible!

pere5 commented Dec 17, 2015

This is absolutely incredible!

@lthai423

This comment has been minimized.

Show comment
Hide comment

+1

@mthadisena

This comment has been minimized.

Show comment
Hide comment
@mthadisena

mthadisena Jan 16, 2016

Thank you so much!

Thank you so much!

@pdp10

This comment has been minimized.

Show comment
Hide comment
@pdp10

pdp10 Jan 27, 2016

Many thanks, very useful.

pdp10 commented Jan 27, 2016

Many thanks, very useful.

@raincrash

This comment has been minimized.

Show comment
Hide comment

Thanks!

@joshlbaker

This comment has been minimized.

Show comment
Hide comment
@joshlbaker

joshlbaker Jan 27, 2016

Thanks! Worked like a charm

Thanks! Worked like a charm

@tetreis

This comment has been minimized.

Show comment
Hide comment

tetreis commented Feb 2, 2016

Thanks!

@michalbiarda

This comment has been minimized.

Show comment
Hide comment

Thanks.

@rcconsult

This comment has been minimized.

Show comment
Hide comment

+1

@KKirad

This comment has been minimized.

Show comment
Hide comment
@KKirad

KKirad Feb 4, 2016

Thanks..!!

KKirad commented Feb 4, 2016

Thanks..!!

@DoubleDi

This comment has been minimized.

Show comment
Hide comment
@DoubleDi

DoubleDi Feb 16, 2016

Thanks so much!

Thanks so much!

@nav-mike

This comment has been minimized.

Show comment
Hide comment

Thanks!

@carolynfischer

This comment has been minimized.

Show comment
Hide comment

Thank you!

@mshivanna

This comment has been minimized.

Show comment
Hide comment

+1

@BigPa

This comment has been minimized.

Show comment
Hide comment
@BigPa

BigPa Mar 5, 2016

πŸ‘

BigPa commented Mar 5, 2016

πŸ‘

@DaveLindberg

This comment has been minimized.

Show comment
Hide comment

:-)

@gpashis

This comment has been minimized.

Show comment
Hide comment
@gpashis

gpashis Mar 21, 2016

Thanks. Works like a charm.

gpashis commented Mar 21, 2016

Thanks. Works like a charm.

@seoyoochan

This comment has been minimized.

Show comment
Hide comment

πŸ‘

@KITSTuhinChandra

This comment has been minimized.

Show comment
Hide comment
@KITSTuhinChandra

KITSTuhinChandra 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.

"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.

Show comment
Hide comment
@poisvin

poisvin Apr 14, 2016

πŸ‘ thank you

poisvin commented Apr 14, 2016

πŸ‘ thank you

@ShuaiyiWu

This comment has been minimized.

Show comment
Hide comment

Thanks

@ve3

This comment has been minimized.

Show comment
Hide comment
@ve3

ve3 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 ....

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.

Show comment
Hide comment
@astromahi

astromahi May 2, 2016

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

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

@greuze

This comment has been minimized.

Show comment
Hide comment
@greuze

greuze May 5, 2016

Cool, worked like a charm πŸ‘

greuze commented May 5, 2016

Cool, worked like a charm πŸ‘

@Vrakfall

This comment has been minimized.

Show comment
Hide comment
@Vrakfall

Vrakfall 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.

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.

Show comment
Hide comment
@mitendraanand

mitendraanand 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).

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.

Show comment
Hide comment
@axedre

axedre May 25, 2016

Brilliant, thanks! πŸ‘

axedre commented May 25, 2016

Brilliant, thanks! πŸ‘

@tbhockey

This comment has been minimized.

Show comment
Hide comment
@tbhockey

tbhockey May 25, 2016

Excellent. thanks.

Excellent. thanks.

@stefanwimmer128

This comment has been minimized.

Show comment
Hide comment
@stefanwimmer128

stefanwimmer128 Jun 13, 2016

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

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

@yen3

This comment has been minimized.

Show comment
Hide comment
@yen3

yen3 Jun 14, 2016

It's very useful. Thank you.

yen3 commented Jun 14, 2016

It's very useful. Thank you.

@PixelAtLarge

This comment has been minimized.

Show comment
Hide comment

Thank you!

@PedramMarandi

This comment has been minimized.

Show comment
Hide comment

Thank you

@gdibble

This comment has been minimized.

Show comment
Hide comment

gdibble commented Jun 30, 2016

πŸ‘

@rifansi

This comment has been minimized.

Show comment
Hide comment
@rifansi

rifansi Jul 4, 2016

thank you

rifansi commented Jul 4, 2016

thank you

@crmpicco

This comment has been minimized.

Show comment
Hide comment

crmpicco commented Jul 8, 2016

πŸ‘

@iamarmanjon

This comment has been minimized.

Show comment
Hide comment

πŸ‘

@deronimo

This comment has been minimized.

Show comment
Hide comment

πŸ‘

@chintan9

This comment has been minimized.

Show comment
Hide comment

thanks

@LeeRuns

This comment has been minimized.

Show comment
Hide comment

LeeRuns commented Aug 10, 2016

dope

@Sanyambansal76

This comment has been minimized.

Show comment
Hide comment

cool...thanks

@unlessbamboo

This comment has been minimized.

Show comment
Hide comment

Thanks.

@Abdelalim-dev

This comment has been minimized.

Show comment
Hide comment
@Mantoudad

This comment has been minimized.

Show comment
Hide comment

Thanks!

@baurzhan-konurbayev

This comment has been minimized.

Show comment
Hide comment

Nice, thanks!

@marcuslang

This comment has been minimized.

Show comment
Hide comment

+1

@lexigen

This comment has been minimized.

Show comment
Hide comment
@lexigen

lexigen Aug 30, 2016

thank you!

lexigen commented Aug 30, 2016

thank you!

@TamarAmit

This comment has been minimized.

Show comment
Hide comment
@TamarAmit

TamarAmit Sep 7, 2016

Thanks! :)

Thanks! :)

@sudhap

This comment has been minimized.

Show comment
Hide comment
@sudhap

sudhap Sep 12, 2016

It worked, Thanks a lot

sudhap commented Sep 12, 2016

It worked, Thanks a lot

@jm-janzen

This comment has been minimized.

Show comment
Hide comment
@jm-janzen

jm-janzen Sep 25, 2016

Can't believe it was so simple. Thanks!

Can't believe it was so simple. Thanks!

@erlinis

This comment has been minimized.

Show comment
Hide comment
@erlinis

erlinis Sep 28, 2016

Thanks ❀️

erlinis commented Sep 28, 2016

Thanks ❀️

@sheikamp

This comment has been minimized.

Show comment
Hide comment
@sheikamp

sheikamp Oct 6, 2016

Great, thanks.

sheikamp commented Oct 6, 2016

Great, thanks.

@ManuelPe

This comment has been minimized.

Show comment
Hide comment
@ManuelPe

ManuelPe Oct 6, 2016

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

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.

Show comment
Hide comment

πŸ‘

@jaymedavis

This comment has been minimized.

Show comment
Hide comment
@jaymedavis

jaymedavis 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. πŸ‘

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.

Show comment
Hide comment

πŸ‘

@PeterRJones

This comment has been minimized.

Show comment
Hide comment
@PeterRJones

PeterRJones Nov 9, 2016

πŸ‘ Just what I needed.

PeterRJones commented Nov 9, 2016

πŸ‘ Just what I needed.

@AdrianAntunez

This comment has been minimized.

Show comment
Hide comment
@AdrianAntunez

AdrianAntunez Nov 10, 2016

Simple and clear. Thank you so much! ;)

AdrianAntunez commented Nov 10, 2016

Simple and clear. Thank you so much! ;)

@enockoloo6

This comment has been minimized.

Show comment
Hide comment
@enockoloo6

enockoloo6 Nov 18, 2016

great staff man!!

great staff man!!

@jvanasco

This comment has been minimized.

Show comment
Hide comment
@jvanasco

jvanasco 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

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.

Show comment
Hide comment
@giacomolm

giacomolm 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/

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.

Show comment
Hide comment
@parvezk

parvezk 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?

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.

Show comment
Hide comment

πŸ‘

@samplestudio

This comment has been minimized.

Show comment
Hide comment
@samplestudio

samplestudio Dec 15, 2016

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

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

@PolinaKhainson

This comment has been minimized.

Show comment
Hide comment
@PolinaKhainson

PolinaKhainson Dec 16, 2016

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

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

@kelseyll

This comment has been minimized.

Show comment
Hide comment

πŸ‘

@sarkiroka

This comment has been minimized.

Show comment
Hide comment

+1.41

@sonk02

This comment has been minimized.

Show comment
Hide comment
@sonk02

sonk02 Dec 21, 2016

Great..It worked for me

sonk02 commented Dec 21, 2016

Great..It worked for me

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Dec 21, 2016

Thank you very much πŸ’™

ghost commented Dec 21, 2016

Thank you very much πŸ’™

@a-komarev

This comment has been minimized.

Show comment
Hide comment
@a-komarev

a-komarev Dec 23, 2016

One of the most useful tricks.

One of the most useful tricks.

@cassiano0

This comment has been minimized.

Show comment
Hide comment
@cassiano0

cassiano0 Jan 8, 2017

Thank you!

Thank you!

@nikhilvm1993

This comment has been minimized.

Show comment
Hide comment

πŸ‘

@mrakitin

This comment has been minimized.

Show comment
Hide comment

Thanks!

@krthkj

This comment has been minimized.

Show comment
Hide comment
@krthkj

krthkj Jan 26, 2017

Thanks πŸ‘

krthkj commented Jan 26, 2017

Thanks πŸ‘

@twhoward99

This comment has been minimized.

Show comment
Hide comment

+1

@RuanHerculano

This comment has been minimized.

Show comment
Hide comment

thanks

@shanebo

This comment has been minimized.

Show comment
Hide comment

shanebo commented Feb 8, 2017

+1

@Raneves

This comment has been minimized.

Show comment
Hide comment
@Raneves

Raneves Feb 10, 2017

thanks a lot!

Raneves commented Feb 10, 2017

thanks a lot!

@constanzekratel

This comment has been minimized.

Show comment
Hide comment
@constanzekratel

constanzekratel Feb 27, 2017

Thanks πŸ‘ !

Thanks πŸ‘ !

@Wing333

This comment has been minimized.

Show comment
Hide comment
@Wing333

Wing333 Mar 6, 2017

Simple yet useful! Thanks a lot!

Wing333 commented Mar 6, 2017

Simple yet useful! Thanks a lot!

@ChristopherMortensen

This comment has been minimized.

Show comment
Hide comment

Thanks!

@luksrn

This comment has been minimized.

Show comment
Hide comment

luksrn commented Mar 15, 2017

+1

@jayeshbadgujar

This comment has been minimized.

Show comment
Hide comment

Thanks.

@Flowerskater

This comment has been minimized.

Show comment
Hide comment

Goog job !

@v0d0m3r

This comment has been minimized.

Show comment
Hide comment
@v0d0m3r

v0d0m3r Mar 24, 2017

Thank you!

v0d0m3r commented Mar 24, 2017

Thank you!

@rymacivorkuy

This comment has been minimized.

Show comment
Hide comment
@rymacivorkuy

rymacivorkuy Apr 6, 2017

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

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.

Show comment
Hide comment
@gracenellore

gracenellore Apr 9, 2017

Thank you! πŸ‘

Thank you! πŸ‘

@masini4ka

This comment has been minimized.

Show comment
Hide comment
@masini4ka

masini4ka Apr 30, 2017

Thanks!!! Saved me time πŸ‘

Thanks!!! Saved me time πŸ‘

@TkachenkoAA

This comment has been minimized.

Show comment
Hide comment
@TkachenkoAA

TkachenkoAA May 5, 2017

Thanks!!! Saved me lifeπŸ‘

Thanks!!! Saved me lifeπŸ‘

@ssonlucera

This comment has been minimized.

Show comment
Hide comment
@ssonlucera

ssonlucera May 17, 2017

Thank you! πŸ‘

Thank you! πŸ‘

@aliuosio

This comment has been minimized.

Show comment
Hide comment

aliuosio commented Jun 2, 2017

Say yeah

@Teshlen

This comment has been minimized.

Show comment
Hide comment
@Teshlen

Teshlen Jun 12, 2017

Thank you!

Teshlen commented Jun 12, 2017

Thank you!

@Landmaster

This comment has been minimized.

Show comment
Hide comment

Thanks!

@wakedeer

This comment has been minimized.

Show comment
Hide comment
@wakedeer

wakedeer Jun 22, 2017

thanks a lot!!!!!!!

thanks a lot!!!!!!!

@abhishekthorat-tudip

This comment has been minimized.

Show comment
Hide comment

Thanks 1+

@jpkempf

This comment has been minimized.

Show comment
Hide comment
@jpkempf

jpkempf Jul 5, 2017

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

jpkempf commented Jul 5, 2017

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

@jbrant

This comment has been minimized.

Show comment
Hide comment
@jbrant

jbrant 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.

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.

Show comment
Hide comment
@lmurta05

lmurta05 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

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.

Show comment
Hide comment
@ngyunter

ngyunter Jul 13, 2017

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

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

@jilucev

This comment has been minimized.

Show comment
Hide comment
@jilucev

jilucev 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.

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.

Show comment
Hide comment

Thanks +1

@David-II

This comment has been minimized.

Show comment
Hide comment
@David-II

David-II Aug 17, 2017

It works, thank you!

It works, thank you!

@MarcosCM

This comment has been minimized.

Show comment
Hide comment
@MarcosCM

MarcosCM 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

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

Show comment
Hide comment

maksim77 commented Sep 6, 2017

Thx!

@karthickkumar00

This comment has been minimized.

Show comment
Hide comment
@karthickkumar00

karthickkumar00 Oct 21, 2017

It works.Thanks!

It works.Thanks!

@tigusigalpa

This comment has been minimized.

Show comment
Hide comment

@lmurta05 Thanks a lot!

@SikindarMirza

This comment has been minimized.

Show comment
Hide comment

Thanks a lot

@liuliqiang

This comment has been minimized.

Show comment
Hide comment
@liuliqiang

liuliqiang Nov 9, 2017

πŸ‘ Thank you guy...

πŸ‘ Thank you guy...

@squoddam

This comment has been minimized.

Show comment
Hide comment
@squoddam

squoddam Nov 9, 2017

Awesome script!

squoddam commented Nov 9, 2017

Awesome script!

@kevinxh

This comment has been minimized.

Show comment
Hide comment

kevinxh commented Nov 20, 2017

πŸ‘

@imdzeeshan

This comment has been minimized.

Show comment
Hide comment

imdzeeshan commented Nov 29, 2017

@MarcosCM πŸ‘

@XxXtraCookie

This comment has been minimized.

Show comment
Hide comment
@XxXtraCookie

XxXtraCookie Dec 8, 2017

wow, thanks a lot! Saved my life ;p

wow, thanks a lot! Saved my life ;p

@anaray

This comment has been minimized.

Show comment
Hide comment
@anaray

anaray Dec 13, 2017

Thanks a lot ! This is clean.

anaray commented Dec 13, 2017

Thanks a lot ! This is clean.

@auraz

This comment has been minimized.

Show comment
Hide comment

auraz commented Dec 14, 2017

Thanks!

@bitbonk

This comment has been minimized.

Show comment
Hide comment
@bitbonk

bitbonk 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

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.

Show comment
Hide comment
@shuaiming

shuaiming Jan 11, 2018

Thanks a billion!

Thanks a billion!

@alannaidon

This comment has been minimized.

Show comment
Hide comment
@alannaidon

alannaidon Jan 16, 2018

Thank you !!

Thank you !!

@CKGrafico

This comment has been minimized.

Show comment
Hide comment

Thanks!

@murcix

This comment has been minimized.

Show comment
Hide comment
@murcix

murcix Jan 29, 2018

thankssssssss

murcix commented Jan 29, 2018

thankssssssss

@cjleth

This comment has been minimized.

Show comment
Hide comment

cjleth commented Feb 8, 2018

tysm

@umairaslamm

This comment has been minimized.

Show comment
Hide comment
@umairaslamm

umairaslamm Feb 9, 2018

thanks a lot πŸ’―

thanks a lot πŸ’―

@SeunghunSunmoonLee

This comment has been minimized.

Show comment
Hide comment

thank you!

@shopglobal

This comment has been minimized.

Show comment
Hide comment
@shopglobal

shopglobal Feb 17, 2018

Still good! +1

shopglobal commented Feb 17, 2018

Still good! +1

@cozzin

This comment has been minimized.

Show comment
Hide comment
@cozzin

cozzin Mar 8, 2018

thank you!

cozzin commented Mar 8, 2018

thank you!

@PunniyaPrabhu

This comment has been minimized.

Show comment
Hide comment

Thank u

@ygnoh

This comment has been minimized.

Show comment
Hide comment
@ygnoh

ygnoh Mar 15, 2018

thank you

ygnoh commented Mar 15, 2018

thank you

@hezymal

This comment has been minimized.

Show comment
Hide comment
@hezymal

hezymal Mar 15, 2018

Thank You!

hezymal commented Mar 15, 2018

Thank You!

@hugo-marcelo

This comment has been minimized.

Show comment
Hide comment

thanks man!

@TorvaldsDB

This comment has been minimized.

Show comment
Hide comment
@TorvaldsDB

TorvaldsDB Mar 19, 2018

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

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

@ezraabc

This comment has been minimized.

Show comment
Hide comment

ezraabc commented Mar 29, 2018

@TorvaldsDB good catch

@ronan-ganachaud-cbp

This comment has been minimized.

Show comment
Hide comment
@ronan-ganachaud-cbp

ronan-ganachaud-cbp 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

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.

Show comment
Hide comment
@alphaCTzo7G

alphaCTzo7G 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

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.

Show comment
Hide comment
@ffoodd

ffoodd 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.

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.

Show comment
Hide comment

Thank you!

@adamteller-macmillan

This comment has been minimized.

Show comment
Hide comment
@adamteller-macmillan

adamteller-macmillan May 1, 2018

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

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

@mshRoR

This comment has been minimized.

Show comment
Hide comment
@mshRoR

mshRoR May 11, 2018

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

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.

Show comment
Hide comment

Thank a lot!!

@olekhy

This comment has been minimized.

Show comment
Hide comment
@olekhy

olekhy Jun 7, 2018

we @ALL are crank here

olekhy commented Jun 7, 2018

we @ALL are crank here

@japutko

This comment has been minimized.

Show comment
Hide comment
@japutko

japutko Jun 20, 2018

thanks man!

japutko commented Jun 20, 2018

thanks man!

@bherzig-nist

This comment has been minimized.

Show comment
Hide comment
@bherzig-nist

bherzig-nist Jun 20, 2018

Cheers! πŸ‘

Cheers! πŸ‘

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