I accidentally deleted some files in a prior commit and would like to recover them. How can I do this?
I ran this and found exactly what I was looking for:
git whatchanged --diff-filter=DAt the time I made the commit, I should have committed the new/changed files only and ran a reset --hard then to recover the missing files. I have about 100 files that I need to restore.
I don't want to do a straight revert as that will also undo the changes in that commit.
Any ideas?
32 Answers
Check out (clone) to somewhere and do the revert there. Extract your files and delete somewhere afterwards.
1Checkout my gist solution: Display a location summary of all deleted files.
$ git log --diff-filter=D --summary
commit fd87304411b93bbf7414f39251ba5e4134b27755 (HEAD)
Author: Kalema Edgar <>
Date: Wed May 1 14:02:36 2019 +0300 Revert "Version 1.0.0 for app" This reverts commit 81585e50d2523c76f9ce7a4375e9b548895773f0. delete mode 100644 bootstrap.php delete mode 100644 composer.json delete mode 100644 composer.lockFind the last commit that affected the path. The path from where the files were deleted
$ git rev-list -n 1 HEAD -- <file_path>username@servername /htdocs/app ((fd87304...))
$ git rev-list -n 1 HEAD -- . (I am using a dot cause I am in the same directory)
fd87304411b93bbf7414f39251ba5e4134b27755The commit that caused the deletion was fd87304411b93bbf7414f39251ba5e4134b27755.
Checkout the version at that commit to return the files back.
username@servername /htdocs/app ((fd87304...))
$ git checkout fd87304411b93bbf7414f39251ba5e4134b27755^ -- <filepath_to_recover>
$ git checkout fd87304411b93bbf7414f39251ba5e4134b27755^ -- .