2019 update:
Since July 2019, there has been a new command which does exactly this: git restore
.
In git status
, now Git recommends using this command instead of git checkout
as it used to.
While this command can also be used to restore the working tree to a specific commit or to restore the content of the index, by default, the working tree is restored to the state in the index (what is asked here).
So, in order to restore the files matching a pathspec (getting rid of their unstaged changes), you would do:
git restore <pathspec>
And to remove all unstaged changes in the current repository:
git restore .
As with git checkout -- .
, this will only discard changes to files tracked by Git and will not discard any new untracked files. If you want to discard any unstaged changes, including new untracked files, you could run an additional:
git clean -dff
Be very careful with this later command however as you might discard files you did not intend to get rid of.
Note on git restore
: as this is a new command, its man page gives a warning:
This command is experimental. The behavior may change.
So it is possible that this answer might become outdated if the behaviour does change in the future. It might thus be wise to run a quick man git-restore
before using it.