Create a gist now

Instantly share code, notes, and snippets.

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

not--p commented Dec 15, 2014

Mahalo.

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

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

atbyrd commented Mar 31, 2015

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

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

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.

Nice one, ty!

donfak commented Jun 23, 2015

Good tip!

joobei commented Jun 26, 2015

Thank you!!

elfen commented Jun 26, 2015

Thank you!

lefke123 commented Jul 6, 2015

Thank you!

perguth commented Jul 7, 2015

πŸ˜„ Thanks, mate!

Thanks.

biniama commented Jul 14, 2015

+100

yiweig commented Jul 26, 2015

+1

Of great help, thanks a lot.

Perfect!

Much appreciated.

+1

Great!

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

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.

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

1+ this works like charm.

mks-d commented Sep 14, 2015

+1

Noiceeee

+1

Thanks !

Thanks, you made my day!

Awesome!!

Thanks a lot.

ve3 commented Oct 12, 2015

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

thanks for this!

rusikf commented Oct 22, 2015

Awesome, thanks!

terales commented Oct 31, 2015

Thanks a lot!

works like a nah !!!

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

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

@markmevans, also works on Slackware with git 2.6.3.

thanks

dfons commented Dec 10, 2015

Great!

ignlg commented Dec 15, 2015

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

pere5 commented Dec 17, 2015

This is absolutely incredible!

+1

Thank you so much!

pdp10 commented Jan 27, 2016

Many thanks, very useful.

Thanks!

Thanks! Worked like a charm

tetreis commented Feb 2, 2016

Thanks!

Thanks.

+1

KKirad commented Feb 4, 2016

Thanks..!!

Thanks so much!

Thanks!

Thank you!

+1

BigPa commented Mar 5, 2016

πŸ‘

gpashis commented Mar 21, 2016

Thanks. Works like a charm.

πŸ‘

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

πŸ‘ thank you

Thanks

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

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

greuze commented May 5, 2016

Cool, worked like a charm πŸ‘

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

Brilliant, thanks! πŸ‘

Excellent. thanks.

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

yen3 commented Jun 14, 2016

It's very useful. Thank you.

Thank you!

Thank you

gdibble commented Jun 30, 2016

πŸ‘

rifansi commented Jul 4, 2016

thank you

crmpicco commented Jul 8, 2016 edited

πŸ‘

πŸ‘

πŸ‘

thanks

LeeRuns commented Aug 10, 2016

dope

cool...thanks

Thanks.

Thanks!

Nice, thanks!

lexigen commented Aug 30, 2016

thank you!

Thanks! :)

sudhap commented Sep 12, 2016

It worked, Thanks a lot

Can't believe it was so simple. Thanks!

erlinis commented Sep 28, 2016

Thanks ❀️

sheikamp commented Oct 6, 2016

Great, thanks.

ManuelPe commented Oct 6, 2016

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

πŸ‘

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

πŸ‘

PeterRJones commented Nov 9, 2016 edited

πŸ‘ Just what I needed.

AdrianAntunez commented Nov 10, 2016 edited

Simple and clear. Thank you so much! ;)

great staff man!!

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

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

πŸ‘

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

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

πŸ‘

+1.41

sonk02 commented Dec 21, 2016

Great..It worked for me

@ghost

ghost commented Dec 21, 2016

Thank you very much πŸ’™

One of the most useful tricks.

Thank you!

πŸ‘

Thanks!

krthkj commented Jan 26, 2017

Thanks πŸ‘

thanks

shanebo commented Feb 8, 2017

+1

Raneves commented Feb 10, 2017

thanks a lot!

Thanks πŸ‘ !

Wing333 commented Mar 6, 2017

Simple yet useful! Thanks a lot!

luksrn commented Mar 15, 2017

+1

Thanks.

Goog job !

v0d0m3r commented Mar 24, 2017

Thank you!

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

Thank you! πŸ‘

Thanks!!! Saved me time πŸ‘

Thanks!!! Saved me lifeπŸ‘

Thank you! πŸ‘

aliuosio commented Jun 2, 2017

Say yeah

Teshlen commented Jun 12, 2017

Thank you!

Thanks!

thanks a lot!!!!!!!

Thanks 1+

jpkempf commented Jul 5, 2017

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

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

@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

gyunter commented Jul 13, 2017

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

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.

Thanks +1

It works, thank you!

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

Thx!

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