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

93,972

Solution 1

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

Solution 2

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

Solution 3

  • 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.

Solution 4

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.

Solution 5

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:
93,972

Related videos on Youtube

Adrien Joly
Author by

Adrien Joly

Crafting and shipping web products since 2002. 🛠🚢✊ #INTJ --- Now building a crawler at @Algolia

Updated on September 07, 2020

Comments

  • Adrien Joly
    Adrien Joly almost 4 years

    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?

  • Mikaël Mayer
    Mikaël Mayer about 10 years
    Can you explain more? I don't know what reset means for git
  • ddavison
    ddavison about 10 years
    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>
  • Mikaël Mayer
    Mikaël Mayer about 10 years
    So you mean that you can reuse an existing branch?
  • Jeff
    Jeff over 9 years
    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.
  • ddavison
    ddavison over 9 years
    if you fetch another developers branch from remote, and do a git checkout -B then yes, it would merge, or possibly even replace
  • melpomene
    melpomene over 6 years
    This doesn't add any new information over the accepted answer from 2011.
  • melpomene
    melpomene over 6 years
    This doesn't add any new information over the accepted answer from 2011.
  • Pshemy108
    Pshemy108 almost 6 years
    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.
  • melpomene
    melpomene almost 6 years
    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.)
  • wesinat0r
    wesinat0r about 4 years
    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.
  • Akash Verma
    Akash Verma about 4 years
    Let's rather say: "git branch creates the branch but you remain in the current branch FROM WHICH you have checked out."
  • quantum231
    quantum231 about 3 years
    Won't the git checkout used without -b switch, just checkout branch from remote so we have a local copy of it?
  • veritas
    veritas almost 3 years
    @quantum231 correct. -b switches to the new brach copy just created.
  • Anthony Avila
    Anthony Avila over 2 years
    git switch -c BRANCH_NAME also works the same as git checkout -b BRANCH_NAME
  • ZeZNiQ
    ZeZNiQ about 2 years
    @AkashVerma It would helpful for readers if you could elaborate on why that "FROM WHICH" that you insisted on in your response, is significant.