Here are different cases as a reference to help others:
If the deletion has not been committed, the command below will restore the deleted file in the working tree.
$ git checkout -- <file>
You can get a list of all the deleted files in the working tree using the command below.
$ git ls-files --deleted
If the deletion has been committed, find the commit where it happened, then recover the file from this commit.
#find the commit hash where it had this file deleted
$ git rev-list -n 1 HEAD -- <file>
It should give you something like c46e81aa403ecb8a0f7a323a358068345
, now use this commit hash with the parent operator (^
) like so:
$ git checkout <commit>^ -- <file>
Example:
$ git checkout c46e81aa403ecb8a0f7a323a358068345^ -- <file>
In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.
$ git log --diff-filter=D --summary
If you want to just display the list of files:
git log --diff-filter=D --summary | grep "delete mode"