Find a deleted file in Git history
Locate deletion commits, search changed content, follow earlier paths, and restore a file from the appropriate parent snapshot.
Use git log --diff-filter=D --summary to find file deletions in the history reachable from the current revision. Add --all to search from the refs available in your local repository. Git can recover committed content only while the relevant objects remain available; it does not retain every deleted file forever.
Locate the deletion
git log --all --diff-filter=D --summary
Keep the commit headers in the output so each deleted path remains associated with its commit. If you know the directory, narrow the search:
git log --all --diff-filter=D --summary -- src/old-module/
A path can be deleted and recreated several times. Inspect the relevant event instead of assuming the first result represents its entire lifetime. Merge comparisons and history simplification can affect which changes are shown; inspect a merge against its individual parents when necessary.
Search when you remember the content
git log --all -S'calculateDiscount' --oneline
git log --all -G'password|secret' --oneline
-S selects changes that alter the number of occurrences of the given string. A change that moves or edits a line without changing that count may not match. -G selects patches whose added or removed lines match a regular expression. Neither command is simply a full-text search of every historical snapshot.
Follow a known path
git log --all --oneline -- path/to/file.js
git log --all --diff-filter=A -- path/to/file.js
git log --all --diff-filter=D --format='%h %an %ad %s' -- path/to/file.js
git log --follow -p -- path/to/file.js
The first commands identify changes, additions, and deletions. The formatted deletion view shows recorded author information and dates; author metadata is not independent proof of who performed an action. --follow traces one path across detected renames, with limitations around complex history and file splits.
Inspect and restore the earlier content
For an ordinary deletion commit whose first parent contains the file:
git show 'DELETION_COMMIT^:path/to/file.js'
git restore --source='DELETION_COMMIT^' -- path/to/file.js
Replace DELETION_COMMIT with the actual commit ID. ^ selects the first parent; for a merge, choose the parent that contains the intended version. The restore command changes the working tree, so save any current content at that path first. Review the result, stage the file, and commit if you want to bring it back into the branch.
If history appears incomplete, check shallow-clone boundaries and whether the relevant remote refs have been fetched. Unreachable commits may be discoverable through reflogs or object inspection until expiration and cleanup, but --all does not include every unreachable object.
See Git history filtering, git-show, and git-restore.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan