Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • The linked problem was very helpful. I was looking for an answer to why checking out a branch on remote puts me in detached head. The answer is doing git checkout origin/master puts you at a specific revision, whereas git checkout master will resolve to git checkout -b master --track origin/master as documented here.
    – mcp
    Commented Apr 23, 2021 at 13:53
  • @young_souvlaki: Right. Note that the new (in Git 2.23) git switch will tell you that origin/master is not a branch name and thus can only be checked out with the --detach option. The old git checkout assumes you understand this already, and hence inserts --detach for you. The old checkout tries the name as a branch name (and as DWIM mode, though sometimes too late!) but if that fails, proceeds to try it as pathspec and via git rev-parse and will overwrite files or do a detached HEAD checkout correspondingly.
    – torek
    Commented Apr 23, 2021 at 21:48
  • These rules are overly complex and sometimes surprise even experienced Git users. If you have Git 2.23 or later, it's probably wise to retrain yourself to use git switch for branch checkout, and git restore for file checkout. (I still haven't quite done this myself.)
    – torek
    Commented Apr 23, 2021 at 21:49