Blog / git
Your coding agent undid your edits: get them back and stop it
An agent ran git checkout or git restore over your changes. What git can still recover, the habit that makes it recoverable, and the rule that stops it.
On this page
- What can git get back?
- And what if it was never staged?
- The habit that makes it recoverable
- How do I stop the agent reverting changes it didn't make?
- When the other person is a teammate
- Frequently asked questions
- Can I recover changes after git checkout or git restore?
- Why did my coding agent revert my changes?
- How do I stop Claude Code running git checkout or git restore?
- Should I commit before running a coding agent?
- Try it with your team
You changed a file by hand, or a teammate did, and the agent working in the same folder put it back. Its reasoning is usually sensible from where it sits: the tests failed, the file differs from the last commit, and the quickest way to green is git checkout -- file or git restore. It doesn't know those lines were yours. It assumed they were a mess left by an earlier step, maybe its own.
Some of that work can come back and some can't. Here is what git can recover, the habit that makes more of it recoverable, and the rules that stop an agent touching changes it didn't make. We ran every command with git 2.43 and the agent tests with Claude Code 2.1.318.
What can git get back?#
Git only knows about content it has stored. A change that was never committed or staged lived only in your working tree, and a checkout or restore overwrites it with nothing left behind. Staging is the difference. We wrote an edit, staged it, made a second edit on top without staging, then ran what an agent would run:
git add cart.py # first edit, staged
# ...second edit, not staged...
git checkout -- cart.py
cat cart.pydef total(items):
# skip refunded items
return sum(i for i in items if i > 0)git checkout -- file copies the file from the staging area, not from the last commit. The staged edit survived; the unstaged second edit was gone.
Then the harsher version, which resets the staging area too:
git restore --staged --worktree cart.py
cat cart.pydef total(items):
return sum(items)Both edits are gone from the folder. But staging had written the first edit into git's object store, and it stays there as a "dangling" object until git cleans up, which by default is weeks away. Ask git to list it:
git fsck --lost-founddangling blob cd8d817ed604d7374cd4a102c071ec2ddd6dfc40cat .git/lost-found/other/cd8d817ed604d7374cd4a102c071ec2ddd6dfc40def total(items):
# skip refunded items
return sum(i for i in items if i > 0)That is the staged edit, whole. In a real repository you may see many dangling blobs; grep -l for a line you remember writing across .git/lost-found/other/ finds the right one quickly.
And what if it was never staged?#
Then git has nothing. Look in these places instead:
- Your editor's local history. VS Code's Timeline view and JetBrains' Local History keep earlier versions of files you edited in them, separate from git.
- The agent's own record. Agents that checkpoint their edits can only restore what they wrote, not what you wrote. They do keep the conversation, though, and if the agent read the file before reverting it, your version may be quoted there word for word.
- A stash the agent made. Some agents run
git stash"to get a clean tree" and never pop it. Checkgit stash listbefore you assume the work is lost.
The habit that makes it recoverable#
Commit or stage your own work before you hand the folder to an agent. A work-in-progress commit is cheap and you can squash it later:
git add -A
git commit -m "wip: refund handling, not done"After that, nothing the agent does to the working tree can lose it: git reset --hard and git checkout both stop at your commit, and git reflog finds it even if a branch moves. Staging alone is weaker but still better than nothing, as the fsck example shows.
The stronger version is to not share a folder at all. Give the agent its own git worktree, so the files you are editing aren't the files it is editing. One database per git worktree covers the setup.
How do I stop the agent reverting changes it didn't make?#
Two layers: an instruction that changes what it tries, and rules that block the commands if it tries anyway.
The instruction. Add this to CLAUDE.md, or to AGENTS.md for Codex, Cursor and OpenCode:
## Changes you did not make
- Before you edit, run `git status`. Uncommitted changes that you did not
make in this session belong to a person.
- Never discard, revert, stash or overwrite them. If they are in your way,
stop and ask.We tested it on a file with a person's half-finished line in it, a trailing + that broke the tests, and asked Claude Code to make the tests pass. Without the rule, it didn't revert the file, but it did finish the person's line for them by deleting the +, a guess about what they meant. With the rule, it changed nothing and asked:
3. Revert to the committed `sum(items)`. This discards the in-progress
edit, so I won't do it unless you say so.
Which one do you want?The rules. An instruction is a request. A deny rule in .claude/ is enforced:
{
"permissions": {
"deny": [
"Bash(git checkout:*)",
"Bash(git restore:*)",
"Bash(git reset:*)",
"Bash(git stash:*)",
"Bash(git clean:*)"
]
}
}With these in place we asked directly: "Put cart.py back to how it is in the last commit." The command was refused, and the agent said so and stopped:
The command was denied, so I haven't changed `cart.py`. It still has the
uncommitted edit.Denying git checkout also blocks switching branches with it, so allow git switch if the agent needs to change branch. And a deny rule blocks commands, not file edits: an agent could still write the old contents back with its edit tool. That is why the instruction matters too.
When the other person is a teammate#
Everything above assumes the edits were yours and you are at the same machine. The harder case is two people's agents on one project, each treating the other's changes as noise. Separate worktrees and branches fix the files; the agents still need to know who is working on what. Joxo gives everyone's agents one shared task board, so an agent can see that a change belongs to a teammate's task before it touches it, and ask that teammate instead of guessing. See your team with a coding agent each.
Frequently asked questions#
Can I recover changes after git checkout or git restore?#
Only if git stored them. Changes that were staged at some point can be found with git fsck --lost-found and read from .git/lost-found/other. Changes that were never staged or committed are gone from git; try your editor's local history.
Why did my coding agent revert my changes?#
It saw the file differ from the last commit and assumed the difference was a mistake, often its own. Nothing told it a person made that change. A rule in CLAUDE.md or AGENTS.md that uncommitted changes it didn't make belong to a person stops most of it.
How do I stop Claude Code running git checkout or git restore?#
Add deny rules for Bash(git checkout:*), Bash(git restore:*), Bash(git reset:*), Bash(git stash:*) and Bash(git clean:*) to .claude/settings.json. In our test the command was refused and Claude Code stopped. Deny rules block commands, not edits, so keep the instruction too.
Should I commit before running a coding agent?#
Yes. A work-in-progress commit means nothing the agent does to the working tree can lose your work, and git reflog finds it even if a branch moves. Squash it when the work lands.