How do I find and restore a deleted file in a Git repository?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Recovering a deleted file in Git is usually easy if you know how to locate the correct historical revision. The key steps are identifying when the deletion happened, restoring the file content from a known commit, and deciding whether to keep recovery local or commit it to branch history. A structured workflow prevents restoring the wrong version and avoids unsafe history edits.
Understand the Recovery Scenario
First identify which situation you are in.
- File was deleted locally but not committed yet.
- Deletion was committed on current branch.
- Deletion happened in older history and you need a specific version.
- File name changed before deletion, so path history is nontrivial.
Each case has a slightly different command sequence.
Recover an Uncommitted Local Deletion
If file was deleted from working tree and you have not committed, restore from HEAD.
Older equivalent command:
This restores current committed version and drops local deletion.
Find the Commit That Deleted the File
For committed deletions, inspect history filtered to delete events.
That gives the deletion commit hash. To inspect file-related history in more detail:
If file was renamed previously, use follow mode on one path segment.
Use this when straightforward path history appears incomplete.
Restore from the Commit Before Deletion
If commit abc1234 deleted the file, restore from its parent revision.
Then stage and commit recovery.
This approach preserves branch history clearly and is safe in shared repositories.
Restore from a Specific Historical Version
Sometimes you do not want the immediately previous version. You want content from a particular commit.
Preview file content before restore if needed.
This avoids accidental restore of outdated or partially migrated content.
Recover Without Creating a Commit Yet
If you want to compare or test recovered content first, restore into working tree and inspect diff.
Commit only after validating build, tests, and runtime behavior.
Restore Multiple Deleted Files
If a commit removed many files and you need several back, restore each explicitly or restore a directory path.
Be careful with broad restores because they may bring back files you did not intend to recover.
Use revert When Deletion Commit Should Be Inverted
If the deletion commit should be undone as a whole in shared history, git revert is often cleaner than file-by-file restore.
This creates a new commit that inverses prior commit changes. It is safer for collaboration than rewriting history with reset and force-push.
Verify Recovery Before Pushing
Always verify what exactly is being recovered.
Run tests and linters if the file is executable source, configuration, or migrations.
Advanced Recovery: Reflog and Lost Commits
If deletion happened during complex history editing, reflog can recover commit pointers.
Once you identify target reflog entry hash, you can inspect or restore file content from that point.
This is especially useful after accidental resets.
Team-Safe Workflow
In collaborative repositories:
- restore on a dedicated branch
- commit with clear recovery message
- open pull request with context
- avoid force pushes unless explicitly coordinated
This keeps blame history and incident response traceable.
Common Pitfalls
A common pitfall is restoring from deletion commit hash directly instead of its parent, which may not contain the file. Another is skipping content preview and restoring an unintended version. Teams also often rely on destructive history edits for simple file recovery and create unnecessary branch coordination issues. Finally, broad path restores can resurrect obsolete files unless reviewed carefully.
Summary
- Use
git restorefor precise file recovery workflows. - Locate deletion commits with
git log --diff-filter=D. - Restore from deletion commit parent or any specific known-good commit.
- Commit recovery explicitly for safe collaboration.
- Validate recovered content before pushing to shared branches.
Related reading
- How do I get the current branch name in Git?
- How do I make git use the editor of my choice for editing commit messages?
- How do I modify a specific commit?
- How do I push a new local branch to a remote Git repository and track it too?
- How do I remove local (untracked) files from the current Git working tree?
- How do I resolve merge conflicts in a Git repository?
- How do I squash my last N commits together?
- How do you push a tag to a remote repository using Git?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.