Skip to content
GitTree
Git guides3 min read

How to restore a deleted file in Git

The right command depends on one thing: how far the deletion got. Here are the three cases, how to find the commit that removed a file, and what Git cannot bring back.

Written by Kodmora

The software house behind GitTree

Updated
  • Undo and recovery
  • Commit history
On this page

To restore a deleted file that you have not committed yet, run `git restore path/to/file`. If the deletion is already in a commit, find that commit with git log --diff-filter=D -- path/to/file, then run git restore --source=<commit>^ -- path/to/file. Git can bring back anything it once stored, so a file that was committed is almost always recoverable.

Which command fits your case
What happenedCommand
Deleted, not stagedgit restore path/to/file
Deleted and stagedgit restore --staged --worktree path/to/file
Deleted in a commitgit restore --source=<commit>^ -- path/to/file

How do I restore a file I just deleted?

If git status lists the file under “Changes not staged for commit”, copy it back from the index:

git restore src/app.ts

How do I restore a file after git rm?

git rm deletes the file and stages the deletion, so it shows under “Changes to be committed”. Restore both the index and the working folder from HEAD:

git restore --staged --worktree src/app.ts

How do I restore a file deleted in an earlier commit?

First find the commit that deleted it. --diff-filter=D shows only deletions:

git log --oneline --diff-filter=D -- src/app.ts

That commit no longer has the file, but its parent does. The ^ means “the parent of”, so this restores the last version before the deletion:

git restore --source=4f2a9c1^ -- src/app.ts

The file comes back to your working folder as a new change. Review it, then stage and commit it like any other edit. On a Git older than 2.23, git checkout 4f2a9c1^ -- src/app.ts does the same job and also stages the file.

What if I do not remember the full path?

Search every branch for the file name. Keep the quotes, so Git expands the pattern rather than your shell:

git log --all --diff-filter=D --name-only --oneline -- "**/app.ts"

What can Git not bring back?

  • A file that was never staged or committed. Git never stored it. Check your editor’s local history or your system backup.
  • Files removed by `git clean`. They were untracked, so Git keeps no copy.
  • Unstaged edits replaced by `git restore`. The command overwrites them, so run it only when you mean to.

A file that was staged once and then lost can sometimes be found with git fsck --lost-found, which lists blobs that nothing points at. The post on undoing a reset, rebase or commit covers lost commits, the lesson on file history and blame shows how to follow a file back through time, and the guide to discarding and cleaning explains what GitTree saves first.

How to get a file back in GitTree

  1. Press Undo first

    If GitTree discarded or cleaned the file, press Undo in the toolbar. It saves a recovery snapshot under refs/ogt/trash before it removes anything.

  2. Search the history

    Use the commit graph search to find the commit that touched the file, then open that commit to read its version.

  3. Use Lost & Found for lost commits

    Choose Tools → Lost & Found to look for commits that no branch points at any more.

Lost & Found in GitTree lists commits and stashes that no branch points at, so you can bring them back.
Lost & Found in GitTree lists commits and stashes that no branch points at, so you can bring them back.

Frequently asked questions

What is the difference between git restore and git checkout?

git restore was added in Git 2.23 to do one job: restore files. git checkout also switches branches, which made it easy to misuse.

How do I see when a file was deleted?

Run git log --diff-filter=D -- path/to/file. It lists the commit that removed it, with its author and date.

Why does git restore say the pathspec did not match?

Git does not know that path at the source you chose. If the file was deleted in a commit, restore it from the parent of that commit with --source=<commit>^.

Can I restore a deleted folder?

Yes. Pass the folder path instead of a file, for example git restore --source=<commit>^ -- src/utils.

Is a restored file committed automatically?

No. It appears as a change in your working folder. Stage and commit it when you are ready.

Sources

Claims about other products were checked against their official pages on November 7, 2026. Prices and features change; the vendors’ pages are the source of truth.