How to Restore a Deleted File in Git

Accidentally deleted a file in your Git repository and need to get it back? Don’t panic — Git keeps the history, and restoring files is easier than you might think.

This guide walks through multiple methods for recovering deleted files in Git.

🔍 1. Identify the Deleted File and Its Commit

First, find the commit where the file existed before deletion:

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

This shows deleted files and the commits where the deletions occurred.

Or you can inspect the history of a specific file:

git log -- <path/to/file>

♻️ 2. Restore the File Using git checkout

If you know the commit hash where the file still existed:

git checkout <commit_hash>^ -- <path/to/file>

This command checks out the file as it was before deletion.

🕵️‍♂️ 3. Restore from a Branch or Stash (Optional)

If you deleted the file locally (but not committed), and want to get it back:

git restore <path/to/file>

Or if the file was part of a stashed change:

git stash show -p | git apply -R

✅ 4. Stage and Commit the Restored File

After restoring:

git add <path/to/file>
git commit -m "Restore deleted file"

🧠 Pro Tips

  • Use gitk or git log --graph for visual history.
  • Always check your .gitignore — restored files may be ignored if listed there.
  • Consider using a GUI tool like GitKraken or Sourcetree for easier recovery.

📌 Summary

MethodUse When
git checkout <commit>File deleted in past commits
git restoreFile deleted locally but not yet committed
git stashFile was part of a stash

Git keeps everything — you just need to know where to look.