Elena' s AI Blog

Restoring deleted files in Git

05 Dec 2023 (updated: 14 Sep 2026) / 9 minutes to read

Elena Daehnhardt

Matrix fell, Midjourney Nov 2023


TL;DR:
  • Restore deleted Git files: use 'git log --diff-filter=D --summary' to find the deletion commit, then 'git checkout COMMIT_ID~1 -- path/to/file' (or 'git restore --source=COMMIT_ID~1 -- path/to/file' on Git ≥ 2.23) to bring the file back. Git history keeps everything — don't panic.

Previous: Part 9 — Reverting Commits in GitHub

Next: Part 11 — Preserve your local changes on Git Pull

Introduction: Recovering Deleted Files in Git

It was late, the weather was foul, and my Wi-Fi decided to have an opinion. Something went wrong during a sync between two machines, and the next morning I discovered that an entire folder of images had quietly vanished from my Git repository. Not overwritten — deleted. Gone from the working tree and staged as deletions in the last commit.

If this has happened to you, take a breath. Git keeps the entire history of your project, including every file that was ever committed. Nothing is truly lost as long as the deletion was committed (and not just a git rm you haven’t committed yet — that is even easier to undo). Let me walk you through exactly what I did.

How to Restore Deleted Files in Git

Step 1 — Find the commit that deleted your files

Git’s --diff-filter option lets you filter the log to show only commits that match a specific kind of change. The flag D means “deleted”:

git log --diff-filter=D --summary

The --summary flag prints a brief list of files added, deleted, or renamed in each commit — which is exactly what we need. You will see output like this:

commit 45a2d299ef3d....
Author: Elena Daehnhardt <email@gmail.com>
Date:   Wed Nov 29 12:05:06 2023 +0100

    create_references

 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.39.png
 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.45.png
 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.50.png

There it is. Copy the commit hash — you will need it in the next step.

Inspecting what changed at a commit

🔒 Subscribe to keep reading.

Conclusion: Git History as a Time Machine

🔒 Subscribe to keep reading.

References

🔒 Subscribe to keep reading.

You've hit a Deep Dive tutorial.

I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):

Already a subscriber? Use the magic link from your last newsletter, or reset your password.

New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in — use the same email afterwards.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2023) 'Restoring deleted files in Git', daehnhardt.com, 05 December 2023. Available at: https://daehnhardt.com/blog/2023/12/05/git-restoring-deleted-files/
All Posts