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?
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With