Find somethingAlways fixable

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>.

quick fix
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

  1. 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 1
    git log --all --full-history -- "**/PlayerController.cs"
    

    Quote the pattern so your shell doesn't expand it, and use **/ so it matches at any depth.

  2. Find the deletion specifically

    --diff-filter=D limits results to commits that deleted the path, which usually leaves exactly one result.

    step 2
    git log --all --full-history --diff-filter=D -- "**/PlayerController.cs"
    
  3. 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 3
    git show a1b2c3d^:src/PlayerController.cs
    
  4. Restore it

    Checking out a path from a commit writes it into your working directory and stages it, without disturbing anything else.

    step 4
    git checkout a1b2c3d^ -- src/PlayerController.cs
    
    # modern equivalent:
    git restore --source=a1b2c3d^ -- src/PlayerController.cs
    
    git status
    git commit -m "Restore PlayerController"
    
  5. 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 5
    git 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 log

Browse commit history with as much or as little detail as you want.

git show

Display a single commit, tag or file-at-a-commit in full.

git checkout

The old all-in-one: switch branches, restore files, or detach HEAD.

git restore

Throw away working-directory changes or unstage files (the modern, clearer replacement for checkout).

git grep

Search 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

← All Git Rescue guides