338

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

I want the current branch to have a new name.

6
  • 3
    accept an answer @Alex or say why you don't want to accept it. Jun 26, 2014 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" Sep 6, 2014 at 19:47
  • 15
    @J.B. OMG are you saying something may have happened to him???!!!!!!!!!
    – abbood
    Sep 16, 2014 at 9:18
  • 1
    Could the highly voted answer to this be accepted? Jan 10, 2015 at 17:56
  • Possible duplicate of How do I rename a local Git branch? Aug 26, 2017 at 16:08

2 Answers 2

507

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.

10
  • 11
    Or git branch -m other-branch renamed-other-branch if you're not.
    – mipadi
    Oct 5, 2010 at 19:27
  • 33
    Also, if you've pushed the old branch to a remote, you can delete it with git push origin :branchname. That would allow you to push the new one and delete the old one, essentially renaming it on the remote.
    – Jonathan
    Oct 7, 2010 at 14:52
  • 1
    @mipadi, it creates the new branch and don't delete the previous one. Aug 3, 2015 at 9:17
  • git branch (-m | -M) [<oldbranch>] <newbranch> Didn't work for me, giving the syntax error. Aug 3, 2015 at 9:18
  • @MohammedArif With -m, it renames the old branch, so it does delete the previous one. Aug 3, 2015 at 9:35
222

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

4
  • 16
    I think this is the correct answer, the highly voted answer by Richard Feam only covers local repo, this one covers remote. Feb 23, 2016 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. Apr 11, 2016 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 8, 2019 at 15:07
  • This is the perfect answer which handles both remote and local branch renaming, thanks a lot. Apr 25, 2023 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.