How to Git Revert a File to a Specific Commit?

Quick and easy way to revert a file to an older version using git

How to Revert a Single File to an Older Commit?

You can use the following command to revert a file to an older version in another commit simply by specifying the commit hash and the file you wish to revert:

git checkout 45db03581b3b5581ed7cc44dbc -- file/to/restore

Basically, it has the following syntax:

git checkout <commit-ref> -- <filename>

How to Revert Multiple Files to an Older Commit?

If you wish to restore multiple files to a version in an earlier commit, simply append more file names at the end (separated by a space). For example:

git checkout 45db03581b3b5581ed7cc44dbc -- file1/to/restore file2/to/restore

How to Find a Commit Hash?

To find the commit hash of the commit you wish to revert the file back to, you can use the git log command. The result for it would look something like the following:

commit 3ba53ff050ef253058088eff5
Author: Designcise
Date:   Sat Dec 12 15:13:23 2020 +0100

    Updated tests

commit 45db03581b3b5581ed7cc44dbc
Author: Designcise
Date:   Sun Dec 6 22:06:47 2020 +0100

    Code Cleanup


The commit hash is the string that appears right after the word "commit".

Hope you found this post useful. It was published . Please show your love and support by sharing this post.