I can't checkout a specific branch, "detached HEAD state"

I'm going to venture a guess that you have a directory named lexer at the top level. Since git checkout is used both to switch branches and to reset files in the tree, it's probably detecting that you don't have a branch called lexer but you do have a path and selects the second mode. It works for your friend because he already has a lexer branch.

Easiest workaround is probably to create the branch using git branch instead.

git branch --track lexer origin/lexer

should do that for you. You can then use git checkout to switch to it.

Another option might be to use the -- flag to git checkout. I haven't tried it but I think it should work:

git checkout lexer --

When you add --, the word before it is always considered a branch/commit/tree and the word after a path.


Your probably want this:

git checkout -t origin/lexer

From git manual:

As a convenience, --track without -b implies branch creation...

and

-t, --track... When creating a new branch, set up "upstream" configuration... If no -b option is given, the name of the new branch will be derived from the remote-tracking branch


You can't checkout lexer because you don't have the branch in your local repository (and surely a folder with the same name) . You only have the remote branch 'origin/lexer'. First you have to create the local branch :

git checkout -b lexer origin/lexer

A good explaination on the subject: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches

Tags:

Git

Github