How do I rename a local Git branch?

后端 未结 30 1208
轻奢々
轻奢々 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:25

    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
    
    1. Delete the old-name remote branch and push the new-name local branch.

    git push origin :old-name new-name

    1. Reset the upstream branch for the new-name local branch. Switch to the branch and then:

    git push origin -u new-name

    Or for a fast way to do that, you can use these 3 steps:

    # Rename branch locally

    git branch -m old_branch new_branch  
    

    # Delete the old remote branch

    git push origin :old_branch  
    

    # Push the new branch, set local branch to track the new remote

    git push --set-upstream origin new_branch   
    

    Referance: https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html

提交回复
热议问题