How can I reset or revert a file to a specific revision?

git, git-checkout, version-control

Solution

Assuming the hash of the commit you want is `c5f567`:

git checkout c5f567 -- file1/to/restore file2/to/restore

The git checkout man page gives more information.

If you want to revert to the commit before `c5f567`, append `~1` (where 1 is the number of commits you want to go back, it can be anything):

git checkout c5f567~1 -- file1/to/restore file2/to/restore

As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).

For the meaning of `--` in the command, refer to In Git, what does `--` (dash dash) mean?

There is also a new `git restore` command that is specifically designed for restoring working copy files that have been modified. If your git is new enough you can use this command, but the documentation comes with a warning:

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

Because `git restore` is experimental, it should not yet be promoted as the primary answer to this question. When the command is no longer marked as "experimental", then this answer can be amended to promote the use of `git restore`. [At the time of writing, the `git restore` command has been marked as "experimental" for at least four years.]

Problem

How can I revert a modified file to its previous revision at a specific commit hash (which I determined via `git log` and `git diff`)?

Original source

Related problems