Recover a deleted file in git
Using git log
with --follow
and --all
git log --follow --all -- path/to/deleted/file.txt
This will show the commit history for the file, including the commit where it was deleted.
Using git log
with --diff-filter
git log --diff-filter=D --summary
This shows all commits that deleted files. Add the file path to be more specific:
git log --diff-filter=D --summary -- path/to/deleted/file.txt
Using git log
with --oneline
for a cleaner view
git log --oneline --follow --all -- path/to/deleted/file.txt
Using git rev-list
to find the exact commit
git rev-list -n 1 HEAD -- path/to/deleted/file.txt
This returns the SHA of the last commit that affected the file (which would be the deletion commit).
To see what was deleted in that commit
Once you have the commit hash, you can see the actual deletion:
git show <commit-hash> -- path/to/deleted/file.txt
If you don't know the exact path, you have several options to find deleted files:
Search by filename only
git log --diff-filter=D --summary | grep "filename"
This searches through all deletion commits for files containing "filename".
List all deleted files
git log --diff-filter=D --summary
This shows all deleted files across the entire repository history. You can then search through the output.
Search deleted files by pattern
git log --diff-filter=D --summary | grep "\.js$"
This finds all deleted JavaScript files, for example.
Use git log with name-only to see just filenames
git log --diff-filter=D --name-only
This gives you a cleaner list of just the deleted filenames with their commit info.
Search within commit messages and file changes
git log --all --full-history -- "**/filename*"
This searches for files matching the pattern in any directory.
Interactive approach with git log and grep
git log --diff-filter=D --name-only | grep -i "part-of-filename"
If you remember roughly when it was deleted
git log --diff-filter=D --since="2024-01-01" --until="2024-06-01" --summary