Find and restore a deleted file in Git

Locate a deletion commit, restore the file from its parent, stage the recovered file, and distinguish committed deletions from working-tree or index changes.

Best practices
Illustration of Git branches and version history

If the file exists in a commit you still have, you can restore that version. Find the commit that deleted it, then restore from a parent that contains the file:

git log --diff-filter=D --oneline -- path/to/deleted.js

For an ordinary single-parent deletion commit, its first parent is the previous version. Use the deletion commit's hash from your own history in the commands below.

Restore a committed deletion

Suppose the deletion commit is e4f5a6b:

# Inspect the last contents before the deletion.
git show e4f5a6b^:path/to/deleted.js

# Restore the working-tree file, then stage it.
git restore --source=e4f5a6b^ -- path/to/deleted.js
git add -- path/to/deleted.js
git commit -m "Restore deleted.js"

Plain restore --source does not stage the restored file. The older git checkout e4f5a6b^ -- path/to/deleted.js updates both the working tree and index, so its staging behavior differs.

For a merge deletion, inspect its parents and select the parent that contains the desired version; ^ means the first parent, not “the last version” in every graph.

When you do not remember the path

Search changes using a distinctive string from the file:

git log --all -S'functionName' --oneline

-S selects changes in the number of occurrences, which can help locate introduction or removal. To list deletion paths directly:

git log --all --diff-filter=D --name-status --oneline

--all searches history reachable from local refs, including fetched remote-tracking refs. It cannot discover history that was never fetched or has already been removed from the object database.

Undo a deletion before committing

If only the working-tree copy was deleted:

git restore -- path/to/file

If git rm also staged the deletion:

git restore --source=HEAD --staged --worktree -- path/to/file

The first form reads from the index; the second explicitly restores both locations from the current commit.

FAQ

Can I recover a file deleted long ago?

Yes, if a retained commit still contains it. Age and intervening merges do not themselves remove the contents. Shallow clones, history rewrites, unavailable refs, or garbage-collected unreachable objects can limit what is locally recoverable.

What if the file was never committed?

Check a stash, editor local history, filesystem backups, or the recycle bin. Git cannot promise recovery of an arbitrary untracked file it never stored. A file previously staged may have an object in the database, but locating that is a separate recovery task, not a reliable backup strategy.

Can I inspect the content without restoring it?

Yes: git show COMMIT:path/to/file prints a historical file without changing your checkout. Use its historical path if it was renamed.

See git-log, git-restore, git-checkout, and git-show.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting