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?

share|improve this question
up vote 172 down vote accepted

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 checkout BRANCH_NAME  # then switch to the new branch
share|improve this answer

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 is the short for:

git branch name
git checkout name
share|improve this answer
  • 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.
share|improve this answer
    
git branch oldbranch: --> What does this do? – Aadishri Jan 4 '17 at 10:00

Full syntax:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

The [FROM_BRANCH] is optional. If there's no FROM_BRANCH, git will use the current branch.

share|improve this answer

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.

share|improve this answer
1  
Can you explain more? I don't know what reset means for git – Mikaël Mayer Jul 1 '14 at 8:30
1  
From the manual on git: If -B is given, <new_branch> is created if it doesn't exist; otherwise, it is reset. This is the transactional equivalent of $ git branch -f <branch> [<start point>] $ git checkout <branch> – sircapsalot Jul 1 '14 at 22:37
    
So you mean that you can reuse an existing branch? – Mikaël Mayer Jul 2 '14 at 7:33
    
Yessir, it resets the branch you choose to what branch you are CURRENTLY on.. – sircapsalot Jul 2 '14 at 13:53
    
Is checkout -B dangerous if the branch you're switching to is shared by others? I used this recently and it seemed to automatically merge in the changes in my other branch to the branch I switched to. – Jeff Feb 17 '15 at 23:25

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

share|improve this answer
    
This doesn't add any new information over the accepted answer from 2011. – melpomene Nov 19 '17 at 15:03

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 :)

share|improve this answer
1  
This doesn't add any new information over the accepted answer from 2011. – melpomene Nov 19 '17 at 15:02

protected by melpomene Nov 19 '17 at 15:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.