0

The following are two git commands. Both of them create a new branch

git branch new_branch_name

git checkout -b new_branch_name

What is the difference between them?

phd
  • 82,685
  • 13
  • 120
  • 165
shivamag00
  • 661
  • 8
  • 13
  • https://stackoverflow.com/search?q=%5Bgit%5D+difference+%22git+branch%22+%22git+checkout+-b%22 – phd Jan 09 '21 at 10:51

2 Answers2

0

git checkout -b new_branch creates a new branch and checks out the new branch while git branch new_branch creates a new branch but leaves you on the same branch.

In other words git checkout -b NEW_BRANCH does the following for you.


git branch BRANCH_NAME    # create a new branch
git switch BRANCH_NAME    # then switch to the new branch

0

in git branch new_branch:

if you have that new_branch, you will get an error that you already have it. if you have not that new_branch, git will create it for you but DOES NOT switch you into it. (only creates the branch, your current branch doesn't change)

in git checkout -b new_branch:

if you have that branch, you will get an error that you already have. if you have not that branch, git will create that for you AND will move you into it. (your current branch will change to the new_branch)

Mahdi Aryayi
  • 1,090
  • 7
  • 13