Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 776
Adrien Joly Avatar asked Nov 02 '11 21:11

Adrien Joly


People also ask

What is the difference between git branch and git checkout?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

Does git checkout b create a new branch?

The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Next, you just have to specify the name for the branch you want to create. To achieve that, you will run the “git checkout” command with the “-b” option and add “feature” as the branch name.

What does B mean in git?

From the git docs: Specifying -b causes a new branch to be created as if git-branch were called and then checked out. You can think of -b as the flag to use when you want to start working directly in a new branch.

What is difference between git checkout and git switch?

Difference between git checkout and git switch It can also be used to restore changes from a certain commit. But git checkout does more than that. It allows you to copy files from any branch or commit directly into your working tree without switching branches.


2 Answers

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 
like image 116
Fatih Acet Avatar answered Oct 07 '22 10:10

Fatih Acet


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 
like image 25
manojlds Avatar answered Oct 07 '22 10:10

manojlds