Deleted files on a Git branch can usually be recovered because Git rarely destroys the underlying data immediately. When you delete a file and commit the deletion, the file's content still exists in the repository's object database, attached to the earlier commit where it was last present. You can bring it back with git restore, git checkout, or git show as long as that commit is still reachable—or, if it isn't, as long as the objects haven't been pruned. The important qualifiers are: recovery works reliably only after the deletion has been committed or at least staged; uncommitted deletions are a different case; and data can become genuinely unrecoverable after garbage collection or in a fresh clone that never contained the file. A few distinctions determine which of those situations you're in, and they're worth understanding before you start typing commands.

Why deletion in Git is usually reversible

Git is a content-addressable store. Every version of every file that has ever been committed is stored as a blob object, and commits are snapshots that point to trees, which point to blobs. Deleting a file creates a new tree and a new commit that simply doesn't reference the blob anymore. The old blob doesn't vanish; it just becomes unreferenced by the current branch tip. Because of that design, "deleted" in Git usually means "no longer in the current snapshot," not "erased from disk."

That's the core reason recovery is routine. But the blob survives only while something keeps it reachable—a branch, a tag, a reflog entry, a stash, or another commit—or until Git's garbage collection removes unreachable objects. So recovery is about finding a commit that still contains the file, or acting before the object is pruned.

It also matters where the deletion happened. A committed deletion on a local branch, a deletion that only exists in your working directory, and a deletion in a repository you just cloned are three genuinely different problems. The commands overlap, but the available history does not.

The four situations that determine your approach

Before running anything, identify which case you're in. The fix differs.

Situation Is the content recoverable? Typical method
File deleted in working tree, not staged or committed Yes, if it was committed before git restore
Deletion staged or committed on your branch Yes, from the commit that last had it git restore --source= --
Deletion committed, then branch reset or amended Usually, via reflog git reflog, then restore from old commit
File only ever existed in another clone or a deleted branch Sometimes, if objects still exist locally git fsck --lost-found, reflog, or fetch from the other copy

The commands below assume you're working in the repository where the file existed. If you're in a fresh clone that never contained the file, restoring from local history won't work—you need the original repository or a remote that still has the commit.

Recovering a file you deleted but haven't committed

If you removed a file with rm, or through your editor, and haven't staged or committed the change, the last committed version is still in HEAD. Restore it directly:

On older Git versions, the equivalent is:

This copies the version from the index (or from HEAD if the index matches) back into your working tree. Nothing was lost because the committed blob was never touched.

If you staged the deletion with git rm or git add, the file is gone from the index but still in HEAD. Unstage the deletion first, then restore:

A single git restore --source=HEAD --staged --worktree path/to/file.txt also works on recent Git versions. The point is that as long as HEAD still contains the file, recovery is trivial.

Recovering a file after the deletion was committed

This is the most common real question, and it's still straightforward. The commit that deleted the file has a parent—the commit just before the deletion—and that parent's tree contains the file. You don't need to rewrite history or revert the whole commit. Point git restore at the right commit:

HEAD1 means "the first parent of the current commit." If the deletion happened several commits ago, walk back further (HEAD3) or, more reliably, find the commit by path:

That lists commits that touched the file, including the deletion. The commit immediately before the deletion is the one whose tree still has the content. Restore from it by hash:

If you want to inspect the content without restoring it, use git show:

The commit:path syntax reads the blob directly from that commit's tree. It's useful for confirming you've found the right version before writing anything to disk, and it works for deleted files exactly as it does for existing ones.

Two related operations are worth knowing. git checkout -- path/to/file.txt does the same restoration on older Git and still works. And git revert creates a new commit that undoes the deletion—appropriate when you want the removal reversed as a recorded change rather than a working-tree edit. Reverting is the safer choice on shared branches because it doesn't rewrite history.

Restoring a whole directory or many files

The same commands accept directories and pathspecs. To bring back an entire folder as it existed in an earlier commit:

To restore only files matching a pattern:

Be deliberate with broad pathspecs on a working tree that has other uncommitted changes; git restore overwrites working-tree files, and there is no built-in undo for that beyond your editor's history.

When the commit isn't on your branch anymore

Deletion commits are easy to find while they're in the branch history. The harder cases are resets, amended commits, rebases, and deleted branches—operations that move a branch pointer away from commits that contained the file. Those commits become unreachable from any branch, but they usually aren't gone.

Git's reflog records where HEAD and branch tips have pointed recently. git reflog shows a chronological list of movements with hashes and descriptions like reset: moving to HEAD~3 or commit (amend). Find the entry from before the destructive operation, then restore from that hash:

Reflog entries are local and expire—by default, reachable entries are kept for 90 days and unreachable ones for 30, though these defaults can be configured. Within that window, a reset or amend is almost always recoverable on the machine where it happened. The reflog is not cloned or pushed, so this route only works on the original clone.

If the commit isn't in the reflog either—for example, the branch was deleted long ago—Git may still have the objects as unreachable data. git fsck --lost-found scans for dangling commits and blobs and writes them under .git/lost-found/. Dangling commits can then be inspected with git show and restored from if they contain the file. This is a slower, more forensic process, and it works only until garbage collection runs.

The case that catches people: the file was never in your clone

A fresh git clone contains only the objects reachable from the branches and tags you fetched. If a file was deleted in an earlier commit and that commit is still in the branch's history, the clone has it—history is cloned by default. But if the deletion was followed by history rewriting (a force-push after a reset or rebase), or if the file only existed on a branch that was never fetched, a new clone may genuinely not contain the content. In that situation, no local command will recover it; you need the original repository, another developer's clone, a backup, or a server-side copy such as a pull request ref or a fork that still has the commit.

This is the most common reason "recovery doesn't work" for someone following a tutorial. The commands are correct, but the data isn't there. Check whether the deletion commit is in your local history with git log --all -- path/to/file.txt before concluding the content is gone. The --all flag includes all refs, not just the current branch.

Shallow clones (--depth) are a related trap. They truncate history, so older commits—including ones that contain a deleted file—may be absent. git fetch --unshallow restores full history if the remote still has it.

What actually makes recovery impossible

Git doesn't keep deleted data forever. Two mechanisms remove it.

First, garbage collection. git gc (and automatic maintenance, which runs periodically in many setups) prunes unreachable objects after a grace period—by default, loose unreachable objects older than two weeks and unreachable packed objects older than the configured prune expiry. Once a blob is pruned, it's gone from that repository. Committed history that is still reachable is not affected; only commits and blobs that nothing references anymore are candidates.

Second, the absence of the data in the first place, as described above. A repository that never had the blob cannot restore it, no matter how the commands are phrased.

There is also a subtler limit: if you deleted a file and never committed—or even staged—the deletion, and the file's only committed version predates your clone's history truncation, local recovery may still fail despite the working-tree deletion seeming recent. The working tree isn't a version store; only committed blobs are.

Practical recovery sequence

When someone asks how to recover a deleted file, the reliable order is:

1: Check whether it's committed. Run git status. If the deletion is unstaged, git restore . If staged, unstage and restore. 2: Find the last commit that had it. git log --oneline --all -- path/to/file.txt shows every commit touching that path across all refs. The commit before the deletion contains the content. 3: Restore from that commit. git restore --source= -- path/to/file.txt, then verify with git show :path/to/file.txt if you want to preview first. 4: If the commit isn't in the log, check the reflog. git reflog covers resets, amends, and branch switches on the local machine. 5: If it's still missing, look for dangling objects. git fsck --lost-found may surface unreachable commits, but act promptly—garbage collection can remove them. 6: If none of that works, go to the source. Another clone, a backup, a remote fork, or a server-side ref may still have the commit.

The commands are not destructive to history. git restore and git show read from existing commits; they don't rewrite anything. That means it's safe to experiment—restore the file, examine it, and remove it again if it's the wrong version.

Avoiding the situation next time

The same properties that make recovery possible also make it easy to prevent the problem. Deletions that are committed are recoverable for as long as the history exists; deletions that are only in the working tree are recoverable only until you discard the change. Committing early and often—even imperfectly—keeps content in the object database where it can be found later. On shared branches, prefer git revert over history rewriting so the deletion and its reversal stay visible. And if a file matters, remember that a clone is not a backup: history survives in any full clone, but a shallow clone, a rewritten remote, or a pruned repository can lose it.