268

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?

1

7 Answers 7

371

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
3
  • Won't the git checkout used without -b switch, just checkout branch from remote so we have a local copy of it?
    – quantum231
    Commented May 20, 2021 at 13:06
  • @quantum231 correct. -b switches to the new brach copy just created.
    – veritas
    Commented Aug 2, 2021 at 10:02
  • 5
    git switch -c BRANCH_NAME also works the same as git checkout -b BRANCH_NAME Commented Mar 22, 2022 at 22:08
66

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
2
  • 2
    Let's rather say: "git branch creates the branch but you remain in the current branch FROM WHICH you have checked out." Commented May 16, 2020 at 18:03
  • 1
    @AkashVerma It would helpful for readers if you could elaborate on why that "FROM WHICH" that you insisted on in your response, is significant.
    – ZeZNiQ
    Commented May 8, 2022 at 14:51
40

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.

1
  • This is a very important comment. Keep this in mind when working with branches in git Commented Dec 29, 2023 at 8:22
39
  • 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
10

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.

6
  • 2
    Can you explain more? I don't know what reset means for git Commented Jul 1, 2014 at 8:30
  • 3
    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>
    – ddavison
    Commented Jul 1, 2014 at 22:37
  • So you mean that you can reuse an existing branch? Commented Jul 2, 2014 at 7:33
  • 1
    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
    Commented Feb 17, 2015 at 23:25
  • 1
    checkout -B will NOT just switch if the branch already exists, it also resets the target branch to the commit of the previous branch (or the specified commit). this can be dangerous also because rerunning checkout -B on a newer branch could reset the branch commits back to the previous branch, which will remove recent commits if the previous branch was behind.
    – wesinat0r
    Commented Apr 8, 2020 at 17:58
-1

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

3
  • 2
    This doesn't add any new information over the accepted answer from 2011.
    – melpomene
    Commented Nov 19, 2017 at 15:03
  • 1
    It actually does add new information about the <start-point>. Which I personally found quite usefull to create branches on elsewhere located object without having to checkout the object first or move the current branch. Using notations as [FROM_BRANCH] when actually the git-reference is meant is not usefull in my opinion.
    – Pshemy108
    Commented Aug 9, 2018 at 17:06
  • 1
    The optional second argument is not relevant to the question. It's the same between both commands, and OP was asking for the difference. (If you really think it's essential, I would just have added a comment to the accepted answer.)
    – melpomene
    Commented Aug 9, 2018 at 17:19
-1

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

1
  • 4
    This doesn't add any new information over the accepted answer from 2011.
    – melpomene
    Commented Nov 19, 2017 at 15:02

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