How do I rename a local Git branch?

后端 未结 30 1162
轻奢々
轻奢々 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.

    0 讨论(0)
  • 2020-11-22 12:10
    git branch -m old_branch_name new_branch_name
    

    The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any.

    If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):

    git push origin new_branch_name:master (now changes will go to master branch but your local branch name is new_branch_name)

    For more details, see "How to rename your local branch name in Git."

    0 讨论(0)
  • 2020-11-22 12:12

    If you want to change the name of the current branch, run:

    git branch -m [old_branch] [new_branch]
    

    If you want to delete the old remote branch, run:

    git push origin :[old_branch]
    

    If you want to delete the old remote branch and create a new remote branch, run:

    git push origin :old_branch new_branch
    
    0 讨论(0)
  • 2020-11-22 12:13

    Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one.

    From my experience, to rename a local and remote branch in Git you should do the following steps.

    Quoting from Multiple States - Rename a local and remote branch in git

    1. Rename your local branch

    If you are on the branch you want to rename:

    git branch -m new-name
    

    If you are on a different branch:

    git branch -m old-name new-name
    

    2. Delete the old-name remote branch and push the new-name local branch

    git push origin :old-name new-name
    

    3. Reset the upstream branch for the new-name local branch

    git push origin -u new-name
    
    0 讨论(0)
  • 2020-11-22 12:13

    Probably as mentioned by others, this will be a case mismatch in branch naming.

    If you have such a situation, I can guess that you're on Windows which will also lead you to:

    $ git branch -m CaseSensitive casesensitive
    fatal: A branch named 'casesensitive' already exists.
    

    Then you have to do an intermediate step:

    $ git branch -m temporary
    $ git branch -m casesensitive
    

    Nothing more.

    0 讨论(0)
  • 2020-11-22 12:13

    Changing the branch locally is quite easy...

    If you are on the branch you want to change the name for, simply do this:

    git branch -m my_new_branch
    

    Otherwise, if you are on master or any other branch other than the one you'd like to change the name, simply do:

    git branch -m my_old_branch my_new_branch
    

    Also, I create the image below to show this in action on a command line. In this case, you are on master branch, for example:

    0 讨论(0)
提交回复
热议问题