A clean Git repository means git status has nothing to report: no staged edits, no unstaged edits, no untracked files. That word is doing a lot of work in your question, because a file can be "gone" from a clean repository in several very different ways, and each one leads to a different recovery route. If the file was deleted and that deletion was committed, the file lives on in the commit that came before the deletion, so git checkout -- path/to/file or git restore --source= -- path/to/file brings it back. If the evidence was never committed at all, Git itself usually cannot help, and your recovery options shift to the editor, the filesystem, or the remote. The first job is to figure out which situation you're in.
A "clean" status does not mean the repository is empty or broken. It means the working tree matches the index and HEAD, with no untracked leftovers sitting around. Whatever file you lost either exists somewhere in Git's object database (a commit, a stash, the reflog, or a dangling object) or it never entered the repository. Recovery starts with that distinction.
First: Identify Which Kind of Loss You're Dealing With
Before running recovery commands, spend two minutes establishing the facts. The right command for a committed deletion will do nothing for a file that was never tracked, and the wrong command can overwrite the one copy you still have.
Was the file ever committed? Run git log --all --oneline -- path/to/file. If commits appear, the file was tracked at some point, and history-based recovery is available. If the output is empty, either the path is wrong, the file was only ever untracked, or the name changed at some point (in which case add --follow to the log command, though --follow works best with a single path and shows renames).
Was the file actively deleted, or does it simply not exist in this branch? A file can be absent from HEAD without any deletion happening on your branch: it may live on another branch, in a commit only reachable from a remote-tracking ref, or in a repository you cloned shallowly. git log --all will expose commits from all refs; git branch -a shows local and remote-tracking branches, and git tag shows tags.
Is the current status truly clean? Run git status and read it carefully. If you see "nothing to commit, working tree clean," untracked files have either been deleted or are being hidden by .gitignore. Check the ignore angle with git status --ignored and git check-ignore -v path/to/file. A file that is ignored was never visible to Git and was never tracked, so this also tells you it can't be recovered from history.
Did anything else change recently? git reflog lists every position HEAD has held in this clone, including commits that are no longer on any branch. A git stash list shows shelved work. These two commands are the most common rescue route when a file disappeared through a reset, rebase, checkout, or stash drop rather than an ordinary commit.
Once you know whether the file was tracked, and roughly when it vanished, the recovery commands make sense.
Recovering a File Deleted in a Commit
This is the cleanest case. Suppose you ran git rm notes.md, committed the removal, and now git status is clean because the deletion is fully committed. The file's last good version is in the parent of the deletion commit.
Step 1: Find the last commit that contained the file. Run git log --oneline -- notes.md. The newest commit that touches the path is likely the deletion; the one below it is the last version with content.
Step 2: Inspect the file in that commit. Run git show :notes.md (replace with the hash or a ref like HEAD~1). If the content looks right, you've confirmed the target.
Step 3: Restore it into your working tree. Use git restore --source= -- notes.md on Git 2.23 or newer, or the older equivalent git checkout -- notes.md. Both write the file into the working tree and stage it. The file appears, already staged, and git status now shows a new file to commit.
Step 4: Commit the restoration with a message that says what happened, for example git commit -m "Restore notes.md deleted in ". If you only wanted the content for reference and not a commit, copy the file elsewhere and run git restore --staged notes.md followed by git restore notes.md to clear the staged copy.
How to check success: git log --oneline -- notes.md should now show your restore commit plus the original history, and git diff HEAD~1 -- notes.md should show the file reappearing. One caveat: restoring from an old commit brings back the file exactly as it was at that point, not any later edits made before deletion. If the file changed between the commit you chose and the deletion, pick the commit immediately before the deletion instead.
Using the Reflog After a Reset, Rebase, or Accidental Checkout
Resets, rebases, and branch switches can make a file vanish while git status still reports a clean tree. The classic example: you run git reset --hard HEAD~3, abandoning three commits, and a file that only existed in those commits is now gone from the branch. Git hasn't discarded those commits yet; they're still in the object database and referenced by the reflog.
Step 1: Read the reflog. Run git reflog and look for the entry just before the destructive operation, typically described as HEAD@{1} or labeled with the command you ran. Note the hash.
Step 2: Verify the file exists in that commit. Run git show :path/to/file. If you get file content rather than a "path not in the tree" error, you've located it.
Step 3: Restore it. Use git restore --source= -- path/to/file or git checkout -- path/to/file, then commit.
Alternative for whole-commit recovery: If you lost more than one file, create a branch at the old commit with git branch rescue and inspect it with git log rescue --oneline. You can then cherry-pick or restore individual files from that branch at leisure. The old commits remain recoverable through the reflog, but not forever: Git's garbage collection eventually prunes unreachable objects. The default expiry for reflog entries is 90 days, and unreachable objects are pruned after a two-week grace period in many default configurations, so act promptly rather than assuming the reflog is permanent.
How to check success: git cat-file -t should report commit, confirming the object still exists, and the restored file should appear in git status as staged.
Pulling a File Back from a Stash
A clean repository can still have stashed work. Run git stash list. If you see entries, you can inspect and recover files without applying the whole stash.
Step 1: Inspect the stash's contents. Run git stash show --name-only stash@{0} to list changed paths, and git show stash@{0}:path/to/file to print a specific file's content.
Step 2: Restore just one file. Use git checkout stash@{0} -- path/to/file. This stages that file into your working tree without touching the rest of the stash.
Step 3: Keep or drop the stash. If you've recovered everything you need, remove the entry with git stash drop stash@{0}. If you're unsure, leave it; stashes are cheap to keep and dropping is the one irreversible step here.
Watch the quoting: stash@{0} contains braces that some shells treat specially. Quote it as 'stash@{0}' in bash or zsh to be safe.
How to check success: The file should be present and staged, and git stash list should still show the entry until you drop it.
When the File Was Never Committed: What Git Can and Cannot Do
If git log --all -- path/to/file returns nothing and the file was untracked before it disappeared, Git never stored its content. A clean status confirms this: untracked files would have shown up as untracked (unless ignored), and once they're deleted, they leave no trace in the repository. No Git command can reconstruct a file Git never saw.
That said, several indirect routes still exist, and they're worth trying in order of reliability.
Check the remote and other clones. If the file was committed and pushed from another machine or by a colleague, it exists in the remote. Run git fetch --all, then git log --all --oneline -- path/to/file again, or search the remote-tracking branches with git grep origin/main -- path/. A file deleted locally but present on the remote comes back with git checkout origin/main -- path/to/file.
Check other working copies and backups. Other clones on your machine, CI build artifacts, editor local history, and system backups (Time Machine, Windows File History, cloud sync version history) are all out-of-Git recovery paths for untracked files. Most editors keep local history: VS Code's timeline view, JetBrains IDEs' Local History, and Vim's swap and undo files are common places a never-committed file lingers after deletion from disk.
Check the filesystem. On most systems a deleted file is unlinked, not immediately erased, but recovery tools for this are platform-specific, often unreliable on SSDs with TRIM enabled, and outside Git's scope. If the file mattered and backups don't cover it, stop writing to that disk before attempting filesystem-level recovery, because new writes can overwrite the freed blocks.
Prevent the repeat. The durable fix for untracked-file loss is to stage early. Even git add -N path/to/file (intent-to-add) makes the path appear in git diff and gives Git a record of the filename, though not the content. Committing early and often, or stashing before risky operations, keeps files inside Git's recovery net.
Recovery by File State: A Quick Decision Table
The table maps the symptom you observe to the first command to try. Use it as a starting point, then follow the matching section above for the confirmation and commit steps.
| What you observe | Likely situation | First command to try |
|---|---|---|
| git log --all -- path shows the file, deletion is the newest commit | Committed deletion | git show HEAD |
| File vanished after reset --hard, rebase, or branch switch; status clean | Unreachable commit still in reflog | git reflog, then git show :path |
| git stash list is non-empty and you remember stashing | File inside a stash | git show 'stash@{0}':path |
| git log --all -- path shows nothing, status clean | Never tracked, or lost from another clone/remote | git fetch --all then re-check log; check backups and editor history |
| File is ignored and untracked | Never in Git at all | git status --ignored to confirm; rely on backups |
The table encodes the same principle as the sections: establish whether the content lives in a Git object before trying Git commands, because a clean status guarantees the working tree holds no stray copies for you to find.
Retrieving from a Shallow Clone or a Fresh Checkout
Two related situations produce a "missing file" that isn't actually deleted: you cloned with limited history, or you're working in a partial checkout. If you ran git clone --depth 1, you only have the latest commit, so git log -- path shows nothing older even though the file existed in prior commits. Deepen the history with git fetch --unshallow (or git fetch --deepen=), then search again. A sparse checkout can also hide files from the working tree while they remain in the index; check with git sparse-checkout list and re-include the path if needed.
A fresh checkout after git clean -fdx is a common way untracked files disappear. git clean deletes untracked and ignored files, and it is not recoverable through Git because those files were never recorded. This is the main reason to prefer git clean -n (dry run) first and to commit or stash before cleaning.
How to check success: After git fetch --unshallow, git log --oneline -- path/to/file should print the historical commits, and git rev-list --count HEAD should grow from 1 to the full history.
Why "Clean" Doesn't Mean "Empty": Read Status Correctly
People sometimes describe a repository as clean when they mean it contains no files, but the term means something narrower. A clean status has three components: no staged changes between HEAD and the index, no unstaged changes between the index and the working tree, and no untracked files. A deleted, committed file satisfies all three, which is why the deletion doesn't show up in git status at all.
This matters for recovery because git status is the worst tool for finding deleted content. It reports the present, not the past. Use git log, git reflog, and git fsck --lost-found instead. That last command lists dangling commits and blobs, objects not reachable from any ref; it's the deepest recovery tool available, and useful when a commit was orphaned so thoroughly that even the reflog entry scrolled away, though you should treat it as a last resort because the output is noisy and requires manual inspection.
If you deleted a whole directory or branch rather than one file, git fsck --lost-found and the reflog remain the routes; restore the commit with git checkout -- path/to/dir/ once you've identified the right dangling commit by inspecting it with git show.
Failures and Limits You Should Expect
Recovery attempts fail for predictable reasons, and knowing them saves time.
Wrong path or renamed file. git log -- path is literal about paths. If the file was renamed, search by content with git log -S"unique sentence" --oneline --all, which finds commits that added or removed that string anywhere in the tree. The pickaxe search (-S) and the regex variant (-G) are the most reliable way to locate a moved file.
Garbage collection already ran. If the reflog entry expired or you ran git gc --prune=now, unreachable commits may be gone. Committed history on reachable branches survives, but orphaned work may not. Check with git fsck --lost-found before concluding the content is unrecoverable.
The recovery overwrote local edits. git restore --source= -- path writes over the working-tree version without warning. If the current file has changes you want, copy it aside first or run git stash push path before restoring.
The remote doesn't have it either. A file deleted and the deletion pushed means the remote's current branch lacks it; the history on the remote does contain it, but only if the deletion commit is reachable and its parent exists there. git log origin/main -- path from the remote-tracking ref will tell you.
You're in a different clone. Confirm with git remote -v and pwd that you're in the intended repository. A file recovered in the wrong clone is a surprisingly common mistake when multiple projects share similar names.
The file was only ever untracked. Accept the Git limit early and pivot to backups and editor history rather than trying more Git commands. No amount of cleverness with git fsck will produce content Git never stored.
A stop condition: If the file is untracked, backups and editor history have failed, and the data is important enough to matter, stop running commands on that disk and hand it to a data-recovery professional or service. DIY filesystem recovery on an actively used drive risks overwriting the very blocks you're trying to recover.
Making the Next Loss Recoverable
The practical takeaway is to keep risky operations inside Git's reach. Commit untracked files before running git clean, git reset --hard, rebase, or branch surgery, even with a throwaway message. Stash rather than discard. Run git fsck --lost-found occasionally in important repositories to see whether anything valuable is dangling. And remember that a clean status is a statement about the present, not a guarantee that recent work is safe: git add and git commit are the only durable protection for new files.
For most readers, the single command worth memorizing is git log --all --oneline -- path/to/file. It answers "did Git ever know this file?" in one line and routes you to every recovery method in this article. The second is git reflog, which turns most accidental losses of committed work into a five-minute restoration.
FAQ
Can I recover a file that was never added or committed?
Not from the repository. Git stores content only once it's staged and committed. Look instead at the remote, other clones, editor local history (VS Code timeline, JetBrains Local History), cloud-sync version history, and system backups. If those fail and the data matters, consider professional filesystem recovery before writing more data to the disk.
Does git checkout -- file work on older Git versions?
Yes. git checkout has supported the pathspec form for a long time, and it remains available on current versions. git restore --source= -- file is the newer, more explicit alternative introduced in Git 2.23 (2019). Both stage the restored file; neither touches commits.
How long do deleted commits stay recoverable?
Reflog entries expire after 90 days by default, and unreachable objects are typically pruned within about two weeks by automatic garbage collection under common defaults. Configuration can change both windows, so treat any unreachable commit as short-lived and recover it promptly.
What does git status --ignored actually show?
It extends the normal status output to list ignored files, which are otherwise invisible to git status. This is how you check whether a "missing" file was ignored and untracked all along, meaning it never existed in Git's view and history-based recovery can't apply.