Using git to restore a single deleted file

Easily recover a Git-tracked file, even after several commits have passed. You need to know the path of the deleted file within the repo. In this example, suppose it’s “path/to/file.txt”.

This example assumes a Unix-like shell. For simplicity, on Windows use WSL to do this.

myfile=path/to/file.txt

git checkout $(git rev-list -n 1 HEAD -- "$myfile")^ -- "$myfile"