Blog / git
Coding agent deleted a file? Get it back with git
An agent removed a file you need. Restore it whether it was never committed, deleted but not committed, or deleted in a commit, with the exact git commands.
On this page
- First, stop the agent
- Deleted, not committed yet
- Deleted in a commit
- Undo the agent's whole commit
- Never committed, but staged once
- Never seen by git
- Stop it happening again
- Frequently asked questions
- How do I restore a file a coding agent deleted but didn't commit?
- How do I get back a file deleted in an earlier commit?
- Can git recover a file that was never committed?
- Is git revert safe on a pushed branch?
- Try it with your team
You asked the agent to tidy up unused code, and now a file you need is gone. Maybe it removed src/ because nothing imported it yet, or it deleted a whole folder while "cleaning up". Whether you can get it back, and how, depends on one thing: did git ever see the file? We ran every command below with git 2.43 in a scratch repository.
First, stop the agent#
Before you restore anything, stop the run or tell the agent to pause. If it keeps going, it may delete the file again, or build on top of the deletion and commit it. Then check where you stand:
git status --short D src/config.tsA D in the second column means the file was tracked and is missing from your folder, but the deletion isn't committed or staged. That is the easiest case.
Deleted, not committed yet#
Put the file back from your last commit:
git restore src/config.tsgit status --short printed nothing after that, and the file was back with its committed contents. Any edits made to it after the last commit are not coming back this way; those lived only in the file.
If the agent removed many files, or a whole folder, list what's missing first:
git ls-files --deletedsrc/config.ts
src/helpers.tsThen restore all of them in one go:
git restore -- $(git ls-files --deleted)In our test the agent had also written a new notes.md. It stayed where it was: git restore only touches the files you name, so new work the agent did is safe. File names with spaces break the line above; restore those one at a time.
If git status shows the D in the first column instead (D src/), the agent ran git rm and the deletion is staged. Undo both at once:
git restore --staged --worktree src/config.tsDeleted in a commit#
If the agent committed the deletion, the file is still in your history. Find the commit that removed it:
git log --oneline --diff-filter=D -- src/helpers.ts12f17be remove unused codeYou don't need to know the exact path. To see every file deleted on the branch, with the commit that did it:
git log --diff-filter=D --summary --oneline12f17be remove unused code
delete mode 100644 src/helpers.tsThe file's last good version is in the commit just before that one, which git writes as 12f17be^. Restore it from there, staged and in your folder:
git restore --source=12f17be^ --staged --worktree src/helpers.tsA src/helpers.tsLeave out --staged --worktree and git puts the file in your folder only, as untracked (?? src/), which is easy to miss when you commit. With both flags it shows as added, ready to commit:
git commit -m "Bring back helpers.ts"A whole folder works the same way. In our test the agent removed src in a commit called "cleanup"; this brought back every file in it:
git restore --source=HEAD~1 --staged --worktree srcA src/config.ts
A src/helpers.tsUndo the agent's whole commit#
If the commit did nothing but delete things you want, revert it:
git revert --no-edit HEAD[main 9edb4bc] Revert "remove helpers"git revert adds a new commit that undoes the old one, so it's safe on a branch you've already pushed or shared. Use it when the whole commit was wrong. When the commit also had changes you want to keep, restore just the files, as above. If the agent rewrote history or reset your branch rather than deleting files, see recover work after an agent's git reset.
Never committed, but staged once#
A file that was never committed isn't in your history, but if it was ever staged with git add, git kept a copy of its contents. In our test we staged plan.md, then removed it with git rm -f. This found it:
git fsck --lost-founddangling blob f1a453b1e8eb591829e920790c55f41f8992f20cThe contents are now in .git/lost-found/other/, one file per lost blob, named by its hash:
cat .git/lost-found/other/f1a453b1e8eb591829e920790c55f41f8992f20csecret plan v1Git doesn't keep the file name, so you may need to open a few to find yours. Copy the one you want back to its old path. This only finds the version that was staged; edits made after the last git add aren't there. Dangling blobs are removed by git's garbage collection after a couple of weeks, so look soon.
Never seen by git#
A file that was never committed and never staged isn't anywhere in git. Before giving up, check:
- Your editor's local history. VS Code's Timeline view and JetBrains' Local History keep recent versions of files you had open.
- Your system's backups, such as Time Machine on a Mac or File History on Windows.
- The agent's own transcript. If the agent wrote the file, its full contents are often in the conversation where it created it.
- Your trash or recycle bin, in case the file was moved rather than removed.
Stop it happening again#
Commit before you hand the agent a cleanup task. A commit costs nothing and turns every case above into the easy one: git restore from your last commit. On a branch you'll squash later, "checkpoint" is a fine message.
Add a line to your project instructions: "Don't delete files. If you think a file is unused, list it and ask." Agents follow a plain rule like this most of the time, and the list is useful on its own: you decide what goes.
Before you commit an agent's work, look for deletions on purpose:
git diff --summary main...HEADEvery removed file shows as a delete mode line, which is much easier to spot than a missing file in a long diff. If you run several agents, reviewing PRs from several agents has a routine for this. To keep an agent from running destructive commands at all, see stop Claude Code running rm -rf.
Frequently asked questions#
How do I restore a file a coding agent deleted but didn't commit?#
Run git restore with the file's path. It brings back the version from your last commit. Use git ls-files --deleted to list every tracked file that is missing.
How do I get back a file deleted in an earlier commit?#
Find the commit with git log --diff-filter=D -- path, then run git restore --source=<commit>^ --staged --worktree path and commit the result.
Can git recover a file that was never committed?#
Only if it was staged with git add at some point. git fsck --lost-found then writes its contents to .git/lost-found/other. Otherwise check your editor's local history or your backups.
Is git revert safe on a pushed branch?#
Yes. It adds a new commit that undoes the old one and doesn't rewrite history, so teammates and their agents can pull it as usual.