Recover a file you deleted before committing it
I permanently deleted a file that was never committed — can I get it back?
Short answer
It comes down to one question: was the file ever staged with git add? If it was, its contents are sitting in Git's object database as a dangling blob and git fsck will find it. If it was never staged and never committed, Git genuinely never saw the file and cannot help — but your editor's local history almost certainly can.
git fsck --lost-found
Finds every orphaned object Git still holds — recovered files land in .git/lost-found/other/
Does this match your situation?
- You deleted a file (or your editor did) and it never made it into a commit.
- It's not in the Recycle Bin or Trash, or you emptied it.
- git status doesn't mention the file at all.
- git checkout and git restore both say the file doesn't exist.
Step-by-step fix
First, work out whether Git ever saw the file
Git only stores content it has been given. A file becomes recoverable the instant you run git add on it — even if you never committed — because staging writes the contents into .git/objects permanently. If you edited the file and deleted it without ever staging it, Git has nothing to give back and you should jump straight to step 5.
step 1git log --all --full-history -- "**/YourFile.cs"If this prints commits, the file was committed at some point and you're in luck — see 'Find a deleted file in history' instead, it's a much easier fix.
Ask Git for every orphaned object it still holds
git fsck walks the whole object database and reports objects that nothing points to any more. A 'dangling blob' is exactly what a staged-but-never-committed file becomes once the index moves on. With --lost-found, Git writes each one into .git/lost-found/other/ as a real file you can open.
step 2git fsck --lost-foundRecovered blobs get hash-named files in .git/lost-found/other/. Open them in your editor and look for your content.
Search the dangling blobs for a line you remember
If fsck returns dozens of objects, don't open them one by one. Print the first few lines of each blob and look for a class name, function or comment you know was in the file. This turns a pile of hashes into an obvious match in a few seconds.
step 3# Bash / Git Bash / macOS / Linux for blob in $(git fsck --lost-found | awk '/dangling blob/ {print $3}'); do echo "=== $blob" git cat-file -p "$blob" | head -5 doneShow the PowerShell (Windows) version
PowerShell (Windows)git fsck --lost-found | Select-String 'dangling blob' | ForEach-Object { ($_ -split ' ')[2] } | ForEach-Object { "=== $_"; git cat-file -p $_ | Select-Object -First 5 }Write the matching blob back to disk
Once you've identified the right hash, git cat-file -p prints its full contents. Redirect that into a file and your work is back exactly as it was when you staged it. Save it under a new name first so you don't overwrite anything that currently exists.
step 4git cat-file -p a1b2c3d4 > recovered-PlayerController.csRecovering gives you the file as of the last time you staged it — anything you typed after that git add is not in Git.
If Git never had it, use your editor's local history
This is the step most guides skip, and it rescues more files than Git does. Modern editors keep their own timeline of every save, completely independently of version control, and it survives deleting the file. Check it before you accept the loss.
step 5VS Code → Command Palette → "Local History: Find Entry to Restore" JetBrains → right-click the folder → Local History → Show History Unity → Library/ is regenerated, but check Assets/ in your editor's history Windows → right-click the folder → Properties → Previous Versions macOS → Time Machine, if enabledJetBrains IDEs and VS Code both keep local history for days regardless of Git. This is your best chance for a never-staged file.
Why this works
Git is content-addressable: the moment you run git add, the file's contents are hashed and written into .git/objects as a blob, and that blob is never modified again. Committing simply adds a tree and commit object pointing at it. So a staged-but-uncommitted file physically exists in your repository — it just has nothing referencing it, which is what 'dangling' means. Those objects survive until git gc prunes them, typically after two weeks, which is your recovery window. A file that was never staged has no blob, and no amount of Git commands can invent one.
If that didn’t work
- Run git fsck --unreachable --no-reflogs, which surfaces objects the plain scan may treat as still-referenced.
- Check whether the file lives in another clone of the repo, on a colleague's machine, or in a CI build artifact.
- Look in your build output folders — compiled or copied versions of the file may still exist there.
- If it was ever opened in a browser-based editor or shared over chat, the attachment may still be retrievable.
How to stop it happening again
- Commit early and often on a scratch branch — a commit takes a second and makes anything recoverable for 90 days.
- Run git stash -u instead of deleting work in progress; stashes are real commits and are themselves recoverable.
- Turn on your editor's local history and increase its retention.
- Never run git clean -fdx to 'tidy up' without running git clean -nd first to see the list.
Commands used in this guide
git fsckScan the object database for orphaned commits and blobs the reflog no longer lists.
git cat-filePrint the raw contents or type of any object by its hash.
git logBrowse commit history with as much or as little detail as you want.
git stashPark uncommitted work temporarily so you can do something else.
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
Can Git recover a file that was never added or committed?
No. Git only stores content that has been handed to it via git add or git commit. If the file never reached the index, nothing about it exists inside .git and no Git command can bring it back. Your editor's local history, your OS's file-versioning feature, or a backup are the realistic options.
How long do dangling blobs survive before Git deletes them?
Until garbage collection prunes them. By default git gc removes unreachable objects that are older than two weeks, and it can run automatically after ordinary commands. That's why you should run git fsck immediately rather than tomorrow — and avoid running git gc --prune=now, which deletes them instantly.
What is the difference between a dangling blob and a dangling commit?
A blob is the contents of a single file, with no name and no history attached. A commit is a full snapshot of the whole project plus a message and parent. Staged-but-uncommitted work shows up as dangling blobs; deleted branches and dropped stashes show up as dangling commits.
Why does git checkout say the file doesn't exist?
git checkout and git restore can only produce files that exist in a commit or in the index. An uncommitted, unstaged file exists in neither, so from Git's point of view there is nothing to restore — which is why fsck, which bypasses references entirely, is the right tool here.
Related rescue guides
I ran git reset --hard and lost my commits — how do I undo it?
Committed work is almost never really gone. git reflog lists every position HEAD has held, including the one just before your reset, so you can jump straight back to it with git reset --hard HEAD@{1}. Uncommitted changes that the reset wiped are a different story — those are only recoverable if they had been staged at some point.
Read the fix →Always fixableA file was deleted months ago — how do I find and restore it?
git log --all --full-history -- "**/filename" finds every commit that ever touched the file, including the one that deleted it. Restore it from the commit before the deletion with git checkout <hash>^ -- <path>.
Read the fix →Usually recoverableI ran git stash drop (or git stash clear) — can I get the stash back?
Stashes are stored as real commit objects, so dropping one only removes it from the stash list — the commit itself survives as an unreachable object. git fsck --unreachable finds it, and git stash apply <hash> puts the work back.
Read the fix →Rarely recoverable by GitHow do I throw away all my local changes in Git?
git restore . discards edits to tracked files and git clean -fd deletes untracked ones. Both are irreversible for anything that was never staged or committed, so run git stash first if there's any doubt — it takes a second and makes the whole operation undoable.
Read the fix →