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.
Subscribe to unlock the full article ❤️
I keep most of the site completely open. A few unusually detailed tutorials need a free subscriber login so I can keep publishing this kind of work.
Log in to unlock
The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.
Full content temporarily unavailable — refresh in a moment