0

What is the difference between the following commands to undo changes in a local file?

git checkout HEAD <file>

git reset <file>

git checkout -- <file>
Konstantin
  • 2,937
  • 10
  • 41
  • 58
  • 3
    For `reset` vs `checkout`: https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout For `checkout` w/ vs w/o `HEAD`: https://stackoverflow.com/q/54680870/4636715 – vahdet Apr 29 '19 at 09:06
  • Possible duplicate of [What's the difference between "git reset" and "git checkout"?](https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout) – phd Apr 29 '19 at 12:11
  • https://stackoverflow.com/search?q=%5Bgit%5D+difference+checkout+reset – phd Apr 29 '19 at 12:11

1 Answers1

2

From git-checkout we can see that 1. and 3. are the same. Also both the working tree and the index is updated:

git checkout [<tree-ish>] [--] <pathspec>…​

Overwrite paths in the working tree by replacing with the contents [..] in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.

From git-reset, we see that only the index is updated:

git reset [-q] [<tree-ish>] [--] …​

[...] copy entries from <tree-ish> to the index.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • Thank you for your reply! So ```git reset``` will just unstage the file, but ```git checkout``` will discard changes in the working directory. Also, ```--``` and ```HEAD``` are equivalent. – Konstantin Apr 29 '19 at 11:44