Git – How to discard unstaged changes in Git

gitversion-control

How do I discard changes in my working copy that are not in the index?

Best Solution

For all unstaged files in current working directory use:

git checkout -- .

For a specific file use:

git checkout -- path/to/file/to/revert

-- here to remove ambiguity (this is known as argument disambiguation).

For Git 2.23 onwards, one may want to use the more specific

git restore .

resp.

git restore path/to/file/to/revert

that together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation.