


If you have made changes to a particular file and you simply want to discard them, you can use the checkout command like this: git checkout myfile This is useful for examining the past state of the repository, but not what you want if you're actually trying to revert changes. When you check out a specific commit in git, you end up in a detached head state.that is, your working copy no longer reflects the state of a named reference (like "master"). So now we are in the same situation as in the start of this answer: with the commit pointed (directly or indirectly) by the HEAD. The content of the working directory is changed, too, to be in accordance with the appropriate commit (snapshot), i.e. This is the situation after performing those commands:Īs 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).

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): We begin with the same state of the repository (pictures in all quadrants are the same): To better understand situations with attached / detached HEAD, let's show the steps leading to the quadruplet of pictures above. it points to a branch, which in turn points to a commit), the HEAD is attached. If it points to a commit indirectly, (i.e.If it points to a commit directly, the HEAD is detached.it points to a branch).ĭetached HEAD means that it is not attached to any branch, i.e. HEAD is a pointer, and it points - directly or indirectly - to a particular commit:Īttached HEAD means that it is attached to some branch (i.e.
