2012-04-03

git - rename branch (local and remote)

git - rename branch (local and remote)

#rename local branch
git branch -m old-branch-name new-branch-name

#delete remote branch with old name
git push origin :old-branch-name

#create remote renamed branch and setup remote branch tracking (-u flag)
git push -u origin new-branch-name

Links

stackoverflow

8 comments:

  1. Thanks, exactly what i was looking for. I would never have guessed the colon...

    ReplyDelete
  2. Shouldn't it be "git push -u origin new-branch-name"? Otherwise, it pushes the new-branch-name to the old name... Right?

    ReplyDelete
    Replies
    1. No, the difference of the "git push -u origin new-branch-name" is that it also sets up tracking of the remote branch for you local branch. It is usually better to add "-u" when you push the branch for the first time, but it still works without "-u".

      Delete
    2. But without "-u" one will have to enter "git push origin new-branch-name" over and over again, whenever there will be a need to push.

      Delete
    3. I think that when I wrote this post I actually always used "git push origin branch-name" syntax.
      But now I use "git push -u" first time and then "git push origin HEAD", so I agree that this is better and I updated the post. And thanks for your comments!

      Delete