Git recovering deleted files or directories in the past commits

0
2516
Git
Git

For some reasons, you might want to recover some deleted files or directories in the past commits, then this is how to do it.

First is to find out the commit that triggers deletion of the path, since files or directories are not in the HEAD.


$ git rev-list -n 1 HEAD -- FILE_PATH

Okay, found the trigger. What you need to do next is to revert from the commit before the trigger.


$ git checkout TRIGGER_COMMIT^ -- FILE_PATH

All files and directories in the FILE_PATH will be recovered!