What is the difference between the git checkout devel and git checkout –b devel commands?

  • The first transition to the branch, the second to create and go to the devel branch - Ihor Tkachuk

2 answers 2

To create a branch and go straight to it, you can run the git checkout command with the -b :

 $ git checkout -b dev 

This is an abbreviation for:

 $ git branch dev $ git checkout dev 

More details can be found here: Branching in Git

    The difference is that:

    1. with the -b option, the checkout command will first try to create the specified branch ( git branch ветка ), and, if the branch already exists, will return an error:

      fatal: A branch named 'branch' already exists.

    2. without the -b option, there will be no preliminary attempt to create a branch, therefore, if such a branch does not exist yet, an error will be returned:

      error: pathspec 'branch' did not match any file (s) known to git.


    simple rule:

    1. if the branch already exists, use it to switch to it

       $ git checkout ветка 
    2. if the branch does not exist, use it to create and switch to it:

       $ git checkout -b ветка