git checkout
will only update the working tree, that is changes which have not yet been staged using git add ...
.
git reset --hard HEAD
will both update the working tree and the index (staged files).
The more flexible and transparent option is to use git checkout -p
, which will prompt for each section to undo individually, and won't undo changes which have been staged. This is a great choice for undoing bits of code you don't want, while retaining some working tree changes. It also forces you to look at what you're deleting, reducing the chance of an unrecoverable mistake.
Using reset would be more appropriate when you have a large number of working tree and staged changes that you are absolutely sure you want to undo, such as after an accidental folder deletion or a bad git mv
.