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 commented Dec 15, 2014

Mahalo.

@AnneTheAgile

This comment has been minimized.

Copy link

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 commented Jan 30, 2015

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

@antarr

This comment has been minimized.

Copy link

antarr commented Mar 31, 2015

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

@FezVrasta

This comment has been minimized.

Copy link

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 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 commented Jun 22, 2015

Nice one, ty!

@donfakk

This comment has been minimized.

Copy link

donfakk commented Jun 23, 2015

Good tip!

@joobei

This comment has been minimized.

Copy link

joobei commented Jun 26, 2015

Thank you!!

@elfen

This comment has been minimized.

Copy link

elfen commented Jun 26, 2015

Thank you!

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 6, 2015

Thank you!

@perguth

This comment has been minimized.

Copy link

perguth commented Jul 7, 2015

πŸ˜„ Thanks, mate!

@rrolla

This comment has been minimized.

Copy link

rrolla commented Jul 10, 2015

Thanks.

@biniama

This comment has been minimized.

Copy link

biniama commented Jul 14, 2015

+100

@yiweig

This comment has been minimized.

Copy link

yiweig commented Jul 26, 2015

+1

@sebastian-code

This comment has been minimized.

Copy link

sebastian-code commented Jul 28, 2015

Of great help, thanks a lot.

@danmcadams

This comment has been minimized.

Copy link

danmcadams commented Jul 28, 2015

Perfect!

@PerpetualBeta

This comment has been minimized.

Copy link

PerpetualBeta commented Aug 6, 2015

Much appreciated.

@mlbright

This comment has been minimized.

Copy link

mlbright commented Aug 19, 2015

+1

@johnheix-g

This comment has been minimized.

Copy link

johnheix-g commented Aug 23, 2015

Great!

@ryabrody

This comment has been minimized.

Copy link

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 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 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 commented Sep 11, 2015

1+ this works like charm.

@mks-d

This comment has been minimized.

Copy link

mks-d commented Sep 14, 2015

+1

@igor-brishkoski

This comment has been minimized.

Copy link

igor-brishkoski commented Sep 18, 2015

Noiceeee

@Juszczak

This comment has been minimized.

Copy link

Juszczak commented Sep 21, 2015

+1

@jibusayone

This comment has been minimized.

Copy link

jibusayone commented Sep 23, 2015

Thanks !

@DieterReuter

This comment has been minimized.

Copy link

DieterReuter commented Sep 23, 2015

Thanks, you made my day!

@CelticParser

This comment has been minimized.

Copy link

CelticParser commented Sep 24, 2015

Awesome!!

@reachsrirams

This comment has been minimized.

Copy link

reachsrirams commented Sep 29, 2015

Thanks a lot.

@ve3

This comment has been minimized.

Copy link

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 commented Oct 15, 2015

thanks for this!

@rusikf

This comment has been minimized.

Copy link

rusikf commented Oct 22, 2015

Awesome, thanks!

@terales

This comment has been minimized.

Copy link

terales commented Oct 31, 2015

Thanks a lot!

@mustafaerturk

This comment has been minimized.

Copy link

mustafaerturk commented Nov 4, 2015

works like a nah !!!

@markmevans

This comment has been minimized.

Copy link

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 commented Nov 25, 2015

+1

@dashmug

This comment has been minimized.

Copy link

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 commented Dec 3, 2015

@markmevans, also works on Slackware with git 2.6.3.

@VasiRahman

This comment has been minimized.

Copy link

VasiRahman commented Dec 7, 2015

thanks

@dfons

This comment has been minimized.

Copy link

dfons commented Dec 10, 2015

Great!

@ignlg

This comment has been minimized.

Copy link

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 commented Dec 17, 2015

This is absolutely incredible!

@lthai423

This comment has been minimized.

Copy link

lthai423 commented Jan 16, 2016

+1

@mthadisena

This comment has been minimized.

Copy link

mthadisena commented Jan 16, 2016

Thank you so much!

@pdp10

This comment has been minimized.

Copy link

pdp10 commented Jan 27, 2016

Many thanks, very useful.

@raincrash

This comment has been minimized.

Copy link

raincrash commented Jan 27, 2016

Thanks!

@joshlbaker

This comment has been minimized.

Copy link

joshlbaker commented Jan 27, 2016

Thanks! Worked like a charm

@tetreis

This comment has been minimized.

Copy link

tetreis commented Feb 2, 2016

Thanks!

@michalbiarda

This comment has been minimized.

Copy link

michalbiarda commented Feb 3, 2016

Thanks.

@rcconsult

This comment has been minimized.

Copy link

rcconsult commented Feb 3, 2016

+1

@KKirad

This comment has been minimized.

Copy link

KKirad commented Feb 4, 2016

Thanks..!!

@DoubleDi

This comment has been minimized.

Copy link

DoubleDi commented Feb 16, 2016

Thanks so much!

@nav-mike

This comment has been minimized.

Copy link

nav-mike commented Feb 24, 2016

Thanks!

@carolynfischer

This comment has been minimized.

Copy link

carolynfischer commented Feb 29, 2016

Thank you!

@mshivanna

This comment has been minimized.

Copy link

mshivanna commented Mar 5, 2016

+1

@ghost

This comment has been minimized.

Copy link

ghost commented Mar 5, 2016

πŸ‘

@DaveLindberg

This comment has been minimized.

Copy link

DaveLindberg commented Mar 17, 2016

:-)

@gpashis

This comment has been minimized.

Copy link

gpashis commented Mar 21, 2016

Thanks. Works like a charm.

@seoyoochan

This comment has been minimized.

Copy link

seoyoochan commented Mar 25, 2016

πŸ‘

@KITSTuhinChandra

This comment has been minimized.

Copy link

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 commented Apr 14, 2016

πŸ‘ thank you

@ShuaiyiWu

This comment has been minimized.

Copy link

ShuaiyiWu commented Apr 27, 2016

Thanks

@ve3

This comment has been minimized.

Copy link

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 commented May 2, 2016

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

@greuze

This comment has been minimized.

Copy link

greuze commented May 5, 2016

Cool, worked like a charm πŸ‘

@Vrakfall

This comment has been minimized.

Copy link

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 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 commented May 25, 2016

Brilliant, thanks! πŸ‘

@tbhockey

This comment has been minimized.

Copy link

tbhockey commented May 25, 2016

Excellent. thanks.

@stefanwimmer128

This comment has been minimized.

Copy link

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 commented Jun 14, 2016

It's very useful. Thank you.

@PixelAtLarge

This comment has been minimized.

Copy link

PixelAtLarge commented Jun 17, 2016

Thank you!

@PedramMarandi

This comment has been minimized.

Copy link

PedramMarandi commented Jun 29, 2016

Thank you

@gdibble

This comment has been minimized.

Copy link

gdibble commented Jun 30, 2016

πŸ‘

@rifansi

This comment has been minimized.

Copy link

rifansi commented Jul 4, 2016

thank you

@crmpicco

This comment has been minimized.

Copy link

crmpicco commented Jul 8, 2016

πŸ‘

@iamajvillalobos

This comment has been minimized.

Copy link

iamajvillalobos commented Jul 11, 2016

πŸ‘

@deronimo

This comment has been minimized.

Copy link

deronimo commented Jul 13, 2016

πŸ‘

@chintan9

This comment has been minimized.

Copy link

chintan9 commented Jul 29, 2016

thanks

@LeeRuns

This comment has been minimized.

Copy link

LeeRuns commented Aug 10, 2016

dope

@Sanyambansal76

This comment has been minimized.

Copy link

Sanyambansal76 commented Aug 11, 2016

cool...thanks

@unlessbamboo

This comment has been minimized.

Copy link

unlessbamboo commented Aug 22, 2016

Thanks.

@Abdelalim-dev

This comment has been minimized.

Copy link

Abdelalim-dev commented Aug 22, 2016

+1

@Mantoudad

This comment has been minimized.

Copy link

Mantoudad commented Aug 23, 2016

Thanks!

@baurzhan-konurbayev

This comment has been minimized.

Copy link

baurzhan-konurbayev commented Aug 26, 2016

Nice, thanks!

@marcuslang

This comment has been minimized.

Copy link

marcuslang commented Aug 29, 2016

+1

@lexigen

This comment has been minimized.

Copy link

lexigen commented Aug 30, 2016

thank you!

@TamarAmit

This comment has been minimized.

Copy link

TamarAmit commented Sep 7, 2016

Thanks! :)

@sudhap

This comment has been minimized.

Copy link

sudhap commented Sep 12, 2016

It worked, Thanks a lot

@jm-janzen

This comment has been minimized.

Copy link

jm-janzen commented Sep 25, 2016

Can't believe it was so simple. Thanks!

@erlinis

This comment has been minimized.

Copy link

erlinis commented Sep 28, 2016

Thanks ❀️

@sheikamp

This comment has been minimized.

Copy link

sheikamp commented Oct 6, 2016

Great, thanks.

@ManuelPe

This comment has been minimized.

Copy link

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 commented Oct 13, 2016

πŸ‘

@jaymedavis

This comment has been minimized.

Copy link

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 commented Oct 17, 2016

πŸ‘

@PeterRJones

This comment has been minimized.

Copy link

PeterRJones commented Nov 9, 2016

πŸ‘ Just what I needed.

@AdrianAntunez

This comment has been minimized.

Copy link

AdrianAntunez commented Nov 10, 2016

Simple and clear. Thank you so much! ;)

@enockoloo6

This comment has been minimized.

Copy link

enockoloo6 commented Nov 18, 2016

great staff man!!

@jvanasco

This comment has been minimized.

Copy link

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 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 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 commented Dec 8, 2016

πŸ‘

@samplestudio

This comment has been minimized.

Copy link

samplestudio commented Dec 15, 2016

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

@PolinaKhainson

This comment has been minimized.

Copy link

PolinaKhainson commented Dec 16, 2016

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

@kelseyll

This comment has been minimized.

Copy link

kelseyll commented Dec 16, 2016

πŸ‘

@sarkiroka

This comment has been minimized.

Copy link

sarkiroka commented Dec 17, 2016

+1.41

@sonk02

This comment has been minimized.

Copy link

sonk02 commented Dec 21, 2016

Great..It worked for me

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 21, 2016

Thank you very much πŸ’™

@antonkomarev

This comment has been minimized.

Copy link

antonkomarev commented Dec 23, 2016

One of the most useful tricks.

@cassiano0

This comment has been minimized.

Copy link

cassiano0 commented Jan 8, 2017

Thank you!

@vmnikhil

This comment has been minimized.

Copy link

vmnikhil commented Jan 16, 2017

πŸ‘

@mrakitin

This comment has been minimized.

Copy link

mrakitin commented Jan 25, 2017

Thanks!

@krthkj

This comment has been minimized.

Copy link

krthkj commented Jan 26, 2017

Thanks πŸ‘

@twhoward99

This comment has been minimized.

Copy link

twhoward99 commented Jan 30, 2017

+1

@RuanHerculano

This comment has been minimized.

Copy link

RuanHerculano commented Feb 6, 2017

thanks

@shanebo

This comment has been minimized.

Copy link

shanebo commented Feb 8, 2017

+1

@rjesususa

This comment has been minimized.

Copy link

rjesususa commented Feb 10, 2017

thanks a lot!

@constanzekratel

This comment has been minimized.

Copy link

constanzekratel commented Feb 27, 2017

Thanks πŸ‘ !

@Wing333

This comment has been minimized.

Copy link

Wing333 commented Mar 6, 2017

Simple yet useful! Thanks a lot!

@ChristopherMortensen

This comment has been minimized.

Copy link

ChristopherMortensen commented Mar 8, 2017

Thanks!

@luksrn

This comment has been minimized.

Copy link

luksrn commented Mar 15, 2017

+1

@jayeshbadgujar

This comment has been minimized.

Copy link

jayeshbadgujar commented Mar 17, 2017

Thanks.

@Flowerskater

This comment has been minimized.

Copy link

Flowerskater commented Mar 20, 2017

Goog job !

@v0d0m3r

This comment has been minimized.

Copy link

v0d0m3r commented Mar 24, 2017

Thank you!

@rymacivorkuy

This comment has been minimized.

Copy link

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 commented Apr 9, 2017

Thank you! πŸ‘

@masini4ka

This comment has been minimized.

Copy link

masini4ka commented Apr 30, 2017

Thanks!!! Saved me time πŸ‘

@TkachenkoAA

This comment has been minimized.

Copy link

TkachenkoAA commented May 5, 2017

Thanks!!! Saved me lifeπŸ‘

@ssonlucera

This comment has been minimized.

Copy link

ssonlucera commented May 17, 2017

Thank you! πŸ‘

@aliuosio

This comment has been minimized.

Copy link

aliuosio commented Jun 2, 2017

Say yeah

@Teshlen

This comment has been minimized.

Copy link

Teshlen commented Jun 12, 2017

Thank you!

@Landmaster

This comment has been minimized.

Copy link

Landmaster commented Jun 17, 2017

Thanks!

@wakedeer

This comment has been minimized.

Copy link

wakedeer commented Jun 22, 2017

thanks a lot!!!!!!!

@abhishekthorat-tudip

This comment has been minimized.

Copy link

abhishekthorat-tudip commented Jul 1, 2017

Thanks 1+

@jpkempf

This comment has been minimized.

Copy link

jpkempf commented Jul 5, 2017

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

@jbrant

This comment has been minimized.

Copy link

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 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 commented Jul 13, 2017

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

@jilucev

This comment has been minimized.

Copy link

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 commented Jul 15, 2017

Thanks +1

@David-II

This comment has been minimized.

Copy link

David-II commented Aug 17, 2017

It works, thank you!

@mcanalesmayo

This comment has been minimized.

Copy link

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 commented Sep 6, 2017

Thx!

@karthickkumar00

This comment has been minimized.

Copy link

karthickkumar00 commented Oct 21, 2017

It works.Thanks!

@tigusigalpa

This comment has been minimized.

Copy link

tigusigalpa commented Oct 25, 2017

@lmurta05 Thanks a lot!

@SikindarMirza

This comment has been minimized.

Copy link

SikindarMirza commented Oct 31, 2017

Thanks a lot

@liuliqiang

This comment has been minimized.

Copy link

liuliqiang commented Nov 9, 2017

πŸ‘ Thank you guy...

@squoddam

This comment has been minimized.

Copy link

squoddam commented Nov 9, 2017

Awesome script!

@kevinxh

This comment has been minimized.

Copy link

kevinxh commented Nov 20, 2017

πŸ‘

@imdzeeshan

This comment has been minimized.

Copy link

imdzeeshan commented Nov 29, 2017

@MarcosCM πŸ‘

@XxXtraCookie

This comment has been minimized.

Copy link

XxXtraCookie commented Dec 8, 2017

wow, thanks a lot! Saved my life ;p

@anaray

This comment has been minimized.

Copy link

anaray commented Dec 13, 2017

Thanks a lot ! This is clean.

@auraz

This comment has been minimized.

Copy link

auraz commented Dec 14, 2017

Thanks!

@bitbonk

This comment has been minimized.

Copy link

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 commented Jan 11, 2018

Thanks a billion!

@alannaidon

This comment has been minimized.

Copy link

alannaidon commented Jan 16, 2018

Thank you !!

@CKGrafico

This comment has been minimized.

Copy link

CKGrafico commented Jan 16, 2018

Thanks!

@murcix

This comment has been minimized.

Copy link

murcix commented Jan 29, 2018

thankssssssss

@cjleth

This comment has been minimized.

Copy link

cjleth commented Feb 8, 2018

tysm

@umairaslamm

This comment has been minimized.

Copy link

umairaslamm commented Feb 9, 2018

thanks a lot πŸ’―

@SeunghunSunmoonLee

This comment has been minimized.

Copy link

SeunghunSunmoonLee commented Feb 15, 2018

thank you!

@shopglobal

This comment has been minimized.

Copy link

shopglobal commented Feb 17, 2018

Still good! +1

@cozzin

This comment has been minimized.

Copy link

cozzin commented Mar 8, 2018

thank you!

@PunniyaPrabhu

This comment has been minimized.

Copy link

PunniyaPrabhu commented Mar 12, 2018

Thank u

@ygnoh

This comment has been minimized.

Copy link

ygnoh commented Mar 15, 2018

thank you

@hezymal

This comment has been minimized.

Copy link

hezymal commented Mar 15, 2018

Thank You!

@hugo-marcelo

This comment has been minimized.

Copy link

hugo-marcelo commented Mar 15, 2018

thanks man!

@TorvaldsDB

This comment has been minimized.

Copy link

TorvaldsDB commented Mar 19, 2018

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

@ezraabc

This comment has been minimized.

Copy link

ezraabc commented Mar 29, 2018

@TorvaldsDB good catch

@ronan-ganachaud-cbp

This comment has been minimized.

Copy link

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 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 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 commented Apr 27, 2018

Thank you!

@adamteller-macmillan

This comment has been minimized.

Copy link

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 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 commented May 17, 2018

Thank a lot!!

@olekhy

This comment has been minimized.

Copy link

olekhy commented Jun 7, 2018

we @ALL are crank here

@japutko

This comment has been minimized.

Copy link

japutko commented Jun 20, 2018

thanks man!

@bherzig-nist

This comment has been minimized.

Copy link

bherzig-nist commented Jun 20, 2018

Cheers! πŸ‘

@rafaelsm

This comment has been minimized.

Copy link

rafaelsm commented Jun 26, 2018

Thanks!

@lomse

This comment has been minimized.

Copy link

lomse commented Jun 28, 2018

Very helpful

@nathanyoung

This comment has been minimized.

Copy link

nathanyoung commented Jul 6, 2018

Thank you!

@dr-smyslov

This comment has been minimized.

Copy link

dr-smyslov commented Jul 13, 2018

thx!

@proton5000

This comment has been minimized.

Copy link

proton5000 commented Jul 17, 2018

+1

@AsharDweedar

This comment has been minimized.

Copy link

AsharDweedar commented Jul 22, 2018

+1

@hafeez84

This comment has been minimized.

Copy link

hafeez84 commented Aug 3, 2018

thanks alot πŸ‘

@manueldeveloper

This comment has been minimized.

Copy link

manueldeveloper commented Aug 15, 2018

πŸ‘

@qrobin

This comment has been minimized.

Copy link

qrobin commented Sep 19, 2018

Awesome, thank you =)

@hanswimtj

This comment has been minimized.

Copy link

hanswimtj commented Sep 27, 2018

+1

@DmitriyYukhanov

This comment has been minimized.

Copy link

DmitriyYukhanov commented Oct 3, 2018

πŸ”₯

@webuxr

This comment has been minimized.

Copy link

webuxr commented Oct 24, 2018

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

@ranjan-ranjan

This comment has been minimized.

Copy link

ranjan-ranjan commented Oct 26, 2018

thanks a lot

@ayatmaulana

This comment has been minimized.

Copy link

ayatmaulana commented Nov 22, 2018

awesome, very helpful

@gjc9620

This comment has been minimized.

Copy link

gjc9620 commented Dec 5, 2018

git config push.default upstream
It's more convenient to use with it

@anderskitson

This comment has been minimized.

Copy link

anderskitson commented Dec 12, 2018

Awesome thanks
πŸ™Œ

@vishnukantjangid

This comment has been minimized.

@chekit

This comment has been minimized.

Copy link

chekit commented Dec 26, 2018

Cool! Thanks. )

@qzcool

This comment has been minimized.

Copy link

qzcool commented Dec 27, 2018

Cool, thank you!

@felipe1982

This comment has been minimized.

Copy link

felipe1982 commented Jan 22, 2019

My local branch was still tracking the old_branch on the remote. This command works to solve that.

git push --set-upstream origin new_branch:new_branch   # Push the new branch, set local branch to track the new remote
@Motti-Shneor

This comment has been minimized.

Copy link

Motti-Shneor commented Feb 13, 2019

Thanks. It's by far the most concise, clear, and easy to understand and use answer to "how to rename my branch, both local and remote" I've seen so far. Some sites sport elaborate explanations, and some even got it dead wrong... but this one is closest to perfect for vast majority of situations.

@tomholub

This comment has been minimized.

Copy link

tomholub commented Feb 13, 2019

awesome

@sapsalumaa

This comment has been minimized.

Copy link

sapsalumaa commented Feb 28, 2019

worked like a charm, thanks!

@mbraunerDE

This comment has been minimized.

Copy link

mbraunerDE commented Mar 6, 2019

Thank you!

@layamba25

This comment has been minimized.

Copy link

layamba25 commented Mar 8, 2019

You still deserve more "Thank Yous".

@sandeepsyngy

This comment has been minimized.

Copy link

sandeepsyngy commented Mar 13, 2019

Thankyou! Cheers

@ggrrll

This comment has been minimized.

Copy link

ggrrll commented Mar 13, 2019

Thank you very much :)

@nerjamartin

This comment has been minimized.

Copy link

nerjamartin commented Mar 14, 2019

Thanks !!!

@shermaneric

This comment has been minimized.

Copy link

shermaneric commented May 7, 2019

After all this time, I bookmark this and I literally search on "mahalo" to help me find this again and again :)

@rusty-1-shaun

This comment has been minimized.

Copy link

rusty-1-shaun commented Jun 25, 2019

A very useful gist indeed, thank you.

@bitkom-ev

This comment has been minimized.

Copy link

bitkom-ev commented Jul 15, 2019

Great, I love it.

@csIng

This comment has been minimized.

Copy link

csIng commented Aug 19, 2019

Cool!

@FlashNoob98

This comment has been minimized.

Copy link

FlashNoob98 commented Aug 21, 2019

thank you so much!

@StephenChamberlain

This comment has been minimized.

Copy link

StephenChamberlain commented Oct 1, 2019

Nice!

@tjbulick

This comment has been minimized.

Copy link

tjbulick commented Oct 1, 2019

❀️

@AGDholo

This comment has been minimized.

Copy link

AGDholo commented Oct 9, 2019

appreciate

@alienr

This comment has been minimized.

Copy link

alienr commented Nov 4, 2019

Still working ....... :)

@hanscode44

This comment has been minimized.

Copy link

hanscode44 commented Nov 7, 2019

Very good! ;-)

@lttlrck

This comment has been minimized.

Copy link
Owner Author