Find and restore a file that was deleted in an old commit
A file was deleted months ago — how do I find and restore it?
Short answer
git log --all --full-history -- "**/filename" finds every commit that ever touched the file, including the one that deleted it. Restore it from the commit before the deletion with git checkout <hash>^ -- <path>.
git log --all --full-history --diff-filter=D -- "**/YourFile.cs"
Finds the commit that deleted the file, searching every branch
Does this match your situation?
- A file you need was removed at some point in the past.
- git log on the path shows nothing because the file no longer exists.
- You don't know which branch or commit removed it.
Step-by-step fix
Search every branch for the file's full history
Plain git log follows the current branch and simplifies history, which is why it often shows nothing for a deleted file. --all searches every ref and --full-history stops the simplification that hides the relevant commits.
step 1git log --all --full-history -- "**/PlayerController.cs"Quote the pattern so your shell doesn't expand it, and use **/ so it matches at any depth.
Find the deletion specifically
--diff-filter=D limits results to commits that deleted the path, which usually leaves exactly one result.
step 2git log --all --full-history --diff-filter=D -- "**/PlayerController.cs"Look at the file before it was deleted
The caret means 'the parent of this commit' — the last state in which the file still existed. Print it to check you've found the right version before restoring.
step 3git show a1b2c3d^:src/PlayerController.csRestore it
Checking out a path from a commit writes it into your working directory and stages it, without disturbing anything else.
step 4git checkout a1b2c3d^ -- src/PlayerController.cs # modern equivalent: git restore --source=a1b2c3d^ -- src/PlayerController.cs git status git commit -m "Restore PlayerController"If you don't know the filename, search by content
The pickaxe finds commits where a given string was added or removed, so a remembered class name or function is enough to locate the file.
step 5git log --all -S "class PlayerController" --oneline
Why this works
Git's history simplification exists to make git log readable: by default it hides commits that don't affect the current branch's version of a path. For a deleted file that heuristic hides exactly what you're looking for, which is why --full-history is required. Once you have the deleting commit, its parent still contains the file, and because every commit is a complete snapshot you can extract any single path from it without checking the whole thing out.
How to stop it happening again
- Write descriptive commit messages when deleting files, so the log explains why.
- Delete in dedicated commits rather than burying removals inside large refactors.
- Use tags for milestones so 'the version before we removed X' is easy to name.
Commands used in this guide
git logBrowse commit history with as much or as little detail as you want.
git showDisplay a single commit, tag or file-at-a-commit in full.
git checkoutThe old all-in-one: switch branches, restore files, or detach HEAD.
git restoreThrow away working-directory changes or unstage files (the modern, clearer replacement for checkout).
git grepSearch the contents of tracked files - at any point in history.
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
How do I find when a file was deleted in Git?
git log --all --full-history --diff-filter=D -- "**/filename" lists the commits that deleted that path across all branches. --full-history is essential, because Git's default history simplification hides those commits.
How do I restore a deleted file from an old commit?
git checkout <deleting-commit>^ -- path/to/file, where the caret selects the parent — the last commit in which the file still existed. The modern form is git restore --source=<commit>^ -- path/to/file.
Why does git log show nothing for my deleted file?
Because git log simplifies history by default and only follows the current branch. Add --all to search every branch and --full-history to disable the simplification that hides deletion commits.
Related rescue guides
How do I revert just one file to a previous version?
git restore --source=<commit> -- <path> rewrites one file to its state at that commit and leaves everything else alone. The classic equivalent is git checkout <commit> -- <path>. Neither moves your branch or affects any other file.
Read the fix →Sometimes recoverableI permanently deleted a file that was never committed — can I get it back?
It comes down to one question: was the file ever staged with git add? If it was, its contents are sitting in Git's object database as a dangling blob and git fsck will find it. If it was never staged and never committed, Git genuinely never saw the file and cannot help — but your editor's local history almost certainly can.
Read the fix →Always fixableHow do I find which commit broke my code?
git bisect binary-searches your history. Tell it one commit that works and one that doesn't; it checks out the midpoint and asks you to test, halving the range each time. With git bisect run it tests automatically and names the guilty commit unattended.
Read the fix →Usually recoverableI deleted a Git branch — how do I get it back?
Deleting a branch removes a pointer, not the commits. Find the branch's last commit hash in the reflog and recreate the branch at that hash with git branch <name> <hash>. If the branch was pushed, the copy on the server may still exist and can simply be fetched back.
Read the fix →