What is the difference between “git branch” and “git checkout -b”?

后端 未结 7 1625
礼貌的吻别
礼貌的吻别 2020-11-29 16:18

I used git checkout -b to create a new branch. I think that git branch does the same thing. How do these two commands differ, if they differ at all

相关标签:
7条回答
  • 2020-11-29 16:26

    There is also another flag to mention, which is relative to these..

    git checkout -B BRANCH_NAME
    

    This is a very useful command that i've been using recently. This command checks out the branch you specify, and resets the branch based on the source branch.

    0 讨论(0)
  • 2020-11-29 16:27

    Essentially :

    A-git branch lets you create a branch plain and simple.

    B -git checkout -b allows you to create a branch and switch to it at the same time.

    When will you use which ? 1- git branch when you want to create a branch but stay on the current branch. 2- git checkout -b when you want to create and switch. If you look at it is intuitive to create a branch and switch to it. So the choice is yours :)

    0 讨论(0)
  • 2020-11-29 16:37

    git branch creates the branch but you remain in the current branch that you have checked out.

    git checkout -b creates a branch and checks it out.

    It could be considered a short form of:

    git branch name
    git checkout name
    
    0 讨论(0)
  • 2020-11-29 16:45
    • git branch: Shows all your branches
    • git branch newbranch: Creates a new branch
    • git checkout -b newbranch: Creates a new branch and switches to that branch immediately. This is the same as git branch newbranch followed by git checkout newbranch.
    0 讨论(0)
  • 2020-11-29 16:46

    There are forms of both commands that are similar (looking at git-scm docs Version 2.11.1):

    git branch <branchname> <start-point>
    

    and

    git checkout -b <new_branch> <start_point>
    

    The latter executing the branch command first and then adding the checkout. In that form explicitly references to git-branch's doc:

    Specifying -b causes a new branch to be created as if git-branch[2] were called and then checked out

    0 讨论(0)
  • 2020-11-29 16:47

    git checkout -b BRANCH_NAME creates a new branch and checks out the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch.

    In other words git checkout -b BRANCH_NAME does the following for you.

    git branch BRANCH_NAME    # create a new branch
    git switch BRANCH_NAME    # then switch to the new branch
    
    0 讨论(0)
提交回复
热议问题