How do I rename a local Git branch?

后端 未结 30 1441
轻奢々
轻奢々 2020-11-22 11:48

I don\'t want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.

How can I rename a local branch wh

30条回答
  •  时光取名叫无心
    2020-11-22 12:08

    Trying to answer specifically to the question (at least the title).

    You can also rename local branch, but keeps tracking the old name on the remote.

    git branch -m old_branch new_branch
    git push --set-upstream origin new_branch:old_branch
    

    Now, when you run git push, the remote old_branch ref is updated with your local new_branch.

    You have to know and remember this configuration. But it can be useful if you don't have the choice for the remote branch name, but you don't like it (oh, I mean, you've got a very good reason not to like it !) and prefer a clearer name for your local branch.

    Playing with the fetch configuration, you can even rename the local remote-reference. i.e, having a refs/remote/origin/new_branch ref pointer to the branch, that is in fact the old_branch on origin. However, I highly discourage this, for the safety of your mind.

提交回复
热议问题