What is the difference between "git branch" and "git checkout -b"?
git branch
: Shows all your branchesgit branch newbranch
: Creates a new branchgit checkout -b newbranch
: Creates a new branch and switches to that branch immediately. This is the same asgit branch newbranch
followed bygit checkout newbranch
.
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.
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
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