How do I rename a local Git branch?

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

    Before we begin, make sure you’ve selected the branch you want to rename:

    git checkout old-name
    

    If you want to see all of your local branches, use the following command:

    git branch --list
    

    When you’re all clear, follow these steps:

    1. Using the Git rename branch command will require you to add an -m option to your command:

      git branch -m new-name
      
    2. You can also rename a local branch from another branch by using the following two commands:

      git checkout master
      
      git branch -m old-name new-name
      
    3. Lastly, this command will list all — both local and remote — branches to verify that it has been renamed:

      git branch -a
      

    Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these three easy steps:

    1. To start, you will need to rename a local branch by following the previous steps. 2.Then delete the old branch and push the new one. You can do this easily with the following commands:

       git push origin --delete old-name
       git push origin :old-name new-name
      
    2. Reset the upstream branch for your new local branch and you will be all set:

      git push origin -u new-name
      

提交回复
热议问题