325

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

I want the current branch to have a new name.

random
  • 9,571
  • 10
  • 67
  • 79
Alex
  • 3,275
  • 2
  • 14
  • 3

2 Answers2

492

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
  • 24,169
  • 6
  • 56
  • 55
204

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
  • 538
  • 2
  • 9
  • 25
javierdvalle
  • 2,333
  • 1
  • 11
  • 14
  • 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
  • 4
    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
  • 2
    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