How do I rename a local Git branch?

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

    If you want to rename a branch while pointed to any branch, do:

    git branch -m <oldname> <newname>
    

    If you want to rename the current branch, you can do:

    git branch -m <newname>
    

    A way to remember this is -m is for "move" (or mv), which is how you rename files. Adding an alias could also help. To do so, run the following:

    git config --global alias.rename 'branch -m'
    

    If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M, otherwise, git will throw branch already exists error:

    git branch -M <newname>
    
    0 讨论(0)
  • 2020-11-22 12:14

    Just three steps to replicate change in name on remote as well as on GitHub:

    Step 1 git branch -m old_branchname new_branchname

    Step 2 git push origin :old_branchname new_branchname

    Step 3 git push --set-upstream origin new_branchname

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

    For Git GUI users it couldn't be much simpler. In Git GUI, choose the branch name from the drop down list in the "Rename Branch" dialog box created from the menu item Branch:Rename, type a New Name, and click "Rename". I have highlighted where to find the drop down list.

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

    If you want to:

    • Rename the Git repository, run: git branch -m <oldname> <newname>
    • Delete the old branch by: git push origin: old-name new-name
    • Commit it using: git commit <newname>
      • and then push using: git push origin new_branch_name:master
    • If you want to check the status then use: git status
    • If you want to check out then use: git checkout
    0 讨论(0)
  • 2020-11-22 12:15

    All of the previous answers are talking about git branch -m. Of course, it's easy to operate, but for me, it may be a little hard to remember another Git command. So I tried to get the work done by the command I was familiar with. Yeah, you may guessed it.

    I use git branch -b <new_branch_name>. And if you don't want to save the old branch now you can execute git branch -D <old_branch_name> to remove it.

    I know it may be a little tedious, but it's easier to understand and remember. I hope it‘s helpful for you.

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

    Another option is not to use the command line at all. Git GUI clients such as SourceTree take away much of the syntactical learning curve / pain that causes questions such as this one to be amongst the most viewed on Stack Overflow.

    In SourceTree, right click on any local branch in the "Branches" pane on the left and select "Rename ...".

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