Hard reset of a single file
Solution 1:
You can use the following command:
git checkout HEAD -- my-file.txt
... which will update both the working copy of my-file.txt
and its state in the index with that from HEAD.
--
basically means: treat every argument after this point as a file name. More details in this answer. Thanks to VonC for pointing this out.
Solution 2:
Reset to head:
To hard reset a single file to HEAD:
git checkout @ -- myfile.ext
Note that @
is short for HEAD
. An older version of git may not support the short form.
Reset to index:
To hard reset a single file to the index, assuming the index is non-empty, otherwise to HEAD:
git checkout -- myfile.ext
The point is that to be safe, you don't want to leave out @
or HEAD
from the command unless you specifically mean to reset to the index only.