Fix a Git detached head?

前端 未结 23 1375
臣服心动
臣服心动 2020-11-22 05:42

I was doing some work in my repository and noticed a file had local changes. I didn\'t want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I

23条回答
  •  醉话见心
    2020-11-22 06:45

    HEAD is a pointer, and it points — directly or indirectly — to a particular commit:

    Attached  HEAD means that it is attached to some branch (i.e. it points to a branch).
    Detached HEAD means that it is not attached to any branch, i.e. it points directly to some commit.

    In other words:

    • If it points to a commit directly, the HEAD is detached.
    • If it points to a commit indirectly, (i.e. it points to a branch, which in turn points to a commit), the HEAD is attached.

    To better understand situations with attached / detached HEAD, let's show the steps leading to the quadruplet of pictures above.

    We begin with the same state of the repository (pictures in all quadrants are the same):


    Now we want to perform git checkout — with different targets in the individual pictures (commands on top of them are dimmed to emphasize that we are only going to apply those commands):


    This is the situation after performing those commands:

    As you can see, the HEAD points to the target of the git checkout command — to a branch (first 3 images of the quadruplet), or (directly) to a commit (the last image of the quadruplet).

    The content of the working directory is changed, too, to be in accordance with the appropriate commit (snapshot), i.e. with the commit pointed (directly or indirectly) by the HEAD.


    So now we are in the same situation as in the start of this answer:

提交回复
热议问题