Create a gist now

Instantly share code, notes, and snippets.

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

Mahalo.

@AnneTheAgile

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

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

@atbyrd
atbyrd commented Mar 31, 2015

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

@FezVrasta

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

@chrisivens

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

Nice one, ty!

@donfak
donfak commented Jun 23, 2015

Good tip!

@joobei
joobei commented Jun 26, 2015

Thank you!!

@elfen
elfen commented Jun 26, 2015

Thank you!

@lefke123
lefke123 commented Jul 6, 2015

Thank you!

@pguth
pguth commented Jul 7, 2015

πŸ˜„ Thanks, mate!

@djbeater

Thanks.

@biniama
biniama commented Jul 14, 2015

+100

@yiweig
yiweig commented Jul 26, 2015

+1

@sebastian-code

Of great help, thanks a lot.

@danmcadams

Perfect!

@PerpetualBeta

Much appreciated.

@mlbright

+1

@johnheix-g

Great!

@ryabrody

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

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

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

@darol100

1+ this works like charm.

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

+1

@igor-brishkoski

Noiceeee

@Juszczak

+1

@jibusayone

Thanks !

@DieterReuter

Thanks, you made my day!

@CelticParser

Awesome!!

@reachsrirams

Thanks a lot.

@ve3
ve3 commented Oct 12, 2015

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

@newshorts

thanks for this!

@rusikf
rusikf commented Oct 22, 2015

Awesome, thanks!

@terales
terales commented Oct 31, 2015

Thanks a lot!

@mustafaerturk

works like a nah !!!

@markmevans

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

+1

@dashmug
dashmug commented Nov 25, 2015

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

@ryanpcmcquen

@markmevans, also works on Slackware with git 2.6.3.

@VasiRahman

thanks

@dfons
dfons commented Dec 10, 2015

Great!

@ignlg
ignlg commented Dec 15, 2015

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

@pere5
pere5 commented Dec 17, 2015

This is absolutely incredible!

@lthai423

+1

@mthadisena

Thank you so much!

@pdp10
pdp10 commented Jan 27, 2016

Many thanks, very useful.

@raincrash

Thanks!

@joshlbaker

Thanks! Worked like a charm

@tetreis
tetreis commented Feb 2, 2016

Thanks!

@michalbiarda

Thanks.

@rcconsult

+1

@KKirad
KKirad commented Feb 4, 2016

Thanks..!!

@DoubleDi

Thanks so much!

@nav-mike

Thanks!

@carolynfischer

Thank you!

@mshivanna

+1

@BigPa
BigPa commented Mar 5, 2016

πŸ‘

@DaveLindberg

:-)

@gpashis
gpashis commented Mar 21, 2016

Thanks. Works like a charm.

@seoyoochan

πŸ‘

@KITSTuhinChandra

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

@r-vinu
r-vinu commented Apr 14, 2016

πŸ‘ thank you

@ShuaiyiWu

Thanks

@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

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

@greuze
greuze commented May 5, 2016

Cool, worked like a charm πŸ‘

@Vrakfall

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
mitendraanand commented May 18, 2016 edited

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

Brilliant, thanks! πŸ‘

@tbhockey

Excellent. thanks.

@stefanwimmer128

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

@yen3
yen3 commented Jun 14, 2016

It's very useful. Thank you.

@PixelAtLarge

Thank you!

@PedramMarandi

Thank you

@gdibble
gdibble commented Jun 30, 2016

πŸ‘

@rifansi
rifansi commented Jul 4, 2016

thank you

@crmpicco
crmpicco commented Jul 8, 2016 edited

πŸ‘

@iamarmanjon

πŸ‘

@deronimo

πŸ‘

@chintan9

thanks

@LeeRuns
LeeRuns commented Aug 10, 2016

dope

@Sanyambansal76

cool...thanks

@unlessbamboo

Thanks.

@Mantoudad

Thanks!

@baurzhan-konurbayev

Nice, thanks!

@marcuslang

+1

@lexigen
lexigen commented Aug 30, 2016

thank you!

@TamarAmit

Thanks! :)

@sudhap
sudhap commented Sep 12, 2016

It worked, Thanks a lot

@jm-janzen

Can't believe it was so simple. Thanks!

@equintana

Thanks ❀️

@sheikamp
sheikamp commented Oct 6, 2016

Great, thanks.

@ManuelPe
ManuelPe commented Oct 6, 2016

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

@ekta-slit

πŸ‘

@jaymedavis
jaymedavis commented Oct 14, 2016 edited

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

πŸ‘

@PeterRJones
PeterRJones commented Nov 9, 2016 edited

πŸ‘ Just what I needed.

@AdrianAntunez
AdrianAntunez commented Nov 10, 2016 edited

Simple and clear. Thank you so much! ;)

@enockoloo6

great staff man!!

@jvanasco

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

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

πŸ‘

@samplestudio

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

@PolinaKhainson

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

@kelseyll

πŸ‘

@sarkiroka

+1.41

@sonk02
sonk02 commented Dec 21, 2016

Great..It worked for me

@aliAsadi92

Thank you very much πŸ’™

@a-komarev

One of the most useful tricks.

@cassiano0

Thank you!

@nikhilvm1993

πŸ‘

@mrakitin

Thanks!

@krthkj
krthkj commented Jan 26, 2017

Thanks πŸ‘

@twhoward99

+1

@RuanHerculano

thanks

@shanebo
shanebo commented Feb 8, 2017

+1

@Raneves
Raneves commented Feb 10, 2017

thanks a lot!

@constanzekratel

Thanks πŸ‘ !

@Wing333
Wing333 commented Mar 6, 2017

Simple yet useful! Thanks a lot!

@ChristopherMortensen

Thanks!

@luksrn
luksrn commented Mar 15, 2017

+1

@jayeshbadgujar

Thanks.

@Flowerskater

Goog job !

@v0d0m3r
v0d0m3r commented Mar 24, 2017

Thank you!

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