332

How do I rename an existing branch in a Git repo?

I want the current branch to have a new name.

random
  • 9,774
  • 10
  • 66
  • 83
Alex
  • 3,345
  • 2
  • 15
  • 3
  • 3
    accept an answer @Alex or say why you don't want to accept it. – Charlie Parker Jun 26 '14 at 20:29
  • 2
    @CharlieParker It probably has something to do with the fact that this was Alex's only question, and he probably hasn't logged in since: "Last seen Feb 3 '11 at 21:29" – yellow-saint Sep 06 '14 at 19:47
  • 15
    @J.B. OMG are you saying something may have happened to him???!!!!!!!!! – abbood Sep 16 '14 at 09:18
  • 1
    Could the highly voted answer to this be accepted? – Steve Chambers Jan 10 '15 at 17:56
  • Possible duplicate of [How do I rename a local Git branch?](https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch) – Vineet Jain Aug 26 '17 at 16:08
  • Does this answer your question? [How do I rename both a Git local and remote branch name?](https://stackoverflow.com/questions/30590083/how-do-i-rename-both-a-git-local-and-remote-branch-name) – Ricardo May 17 '23 at 07:07

2 Answers2

500

Assuming you're currently on the branch you want to rename:

git branch -m newname

This is documented in the manual for git-branch, which you can view using

man git-branch

or

git help branch

Specifically, the command is

git branch (-m | -M) [<oldbranch>] <newbranch>

where the parameters are:

   <oldbranch>
       The name of an existing branch to rename.

   <newbranch>
       The new name for an existing branch. The same restrictions as for <branchname> apply.

<oldbranch> is optional, if you want to rename the current branch.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
215

If you're currently on the branch you want to rename:

git branch -m new_name 

Or else:

git branch -m old_name new_name 

You can check with:

git branch -a

As you can see, only the local name changed Now, to change the name also in the remote you must do:

git push origin :old_name

This removes the branch, then upload it with the new name:

git push origin new_name

Source: https://web.archive.org/web/20150929104013/http://blog.changecong.com:80/2012/10/rename-a-remote-branch-on-github

styrofoam fly
  • 578
  • 2
  • 9
  • 25
javierdvalle
  • 2,473
  • 1
  • 13
  • 15
  • 16
    I think this is the correct answer, the highly voted answer by Richard Feam only covers local repo, this one covers remote. – user1145404 Feb 23 '16 at 17:40
  • 5
    Agreed with the comment above, this answer was more complete in my case. Also, when I pushed additional commits to the remote branch after doing all the steps mentioned in this answer, git tried to push to the `old_name` branch again. Fortunately, git also supplied a fix in the command line: `git-branch --unset-upstream`. After this, all pushed commits went to the `new_name` remote branch. – Hans Roerdinkholder Apr 11 '16 at 12:26
  • 3
    beware that this way you lost the faculty to push with `git push` because you gent a warning whi says `Your branch is based on 'old_name, but the upstream is gone.` A `git push -u origin new_name` solve it. – netalex Feb 08 '19 at 15:07
  • This is the perfect answer which handles both remote and local branch renaming, thanks a lot. – Shreyasikhar26 Apr 25 '23 at 08:33