AI Coding Agent Keeps Reverting Your Changes? Fix It
Your AI coding agent keeps reverting your changes? Five causes, a symptom table, and a git recovery recipe that gets your edits back in under a minute.
If your AI coding agent keeps reverting your changes, one of five things is happening: it is working from an old copy of the file, it rewrites whole files instead of patching them, a formatter or generator is overwriting your edit, two sessions are fighting over the same files, or the agent decided your change was a bug and "fixed" it. Which one it is shows in the symptom, and each has a fix that does not involve arguing with the model.
First, get your work back if it is gone. Then find the cause.
Recover the lost edit first
If you committed before the revert, or if git tracked the file, you can usually get it back. Git keeps more history than most people realise.
Check the reflog. The git reflog documentation describes it as a record of when the tips of branches and other references were updated in your local repository. Run
git reflogand look for the entry just before things went wrong.HEAD@{2}means where HEAD used to be two moves ago.Restore a single file from that point. The git restore documentation shows the form
git restore --source <commit> <path>, which takes a file out of another commit and writes it into your working tree.Check the stash. If the agent ran a stash,
git stash listshows the entries andgit stash popapplies the latest one on top of your current state, as the git stash documentation explains.
git reflog # find the entry before the revert
git restore --source=HEAD@{2} path/to/file.ts
git stash list # check nothing was stashed awayIf your edit was never committed and the agent overwrote the file, git cannot return it. Your editor's local history may still have it, so look there before you retype anything.
Why an AI coding agent keeps reverting your changes
What you see | Likely cause | Fix |
|---|---|---|
--- | --- | --- |
Your edit vanishes right after the agent edits the same file | The agent read the file before your change and wrote back its old version plus its own edit | Commit first, tell the agent to re-read the file, ask for patches rather than whole files |
Your edit survives, then disappears on save or build | A formatter, linter autofix or code generator rewrites the file | Edit the source of the generated file, or exclude it from the generator |
Edits vanish when you switch tasks | Two sessions or two branches are writing to one working folder | One session per branch, or separate working copies |
Only your test or config change is undone | The agent treated it as a problem to fix | Say why the change exists and put it in the instructions file |
Everything since an hour ago is gone | A stash, checkout or reset ran in the agent's shell | Reflog, then restore |
Most people assume the model is being stubborn. In practice the first two rows cause the bulk of the pain, and neither is about intent.
Stop it happening again
Commit before every agent turn. A checkpoint commit takes seconds and makes every later revert reversible. Squash the noise before you merge. Our guide to using git with an AI coding agent safely sets up the full routine.
Say which files are yours. Put a short list in the agent's instructions file: files you edit by hand, files that are generated, files that must not change. If the agent ignores such a list, this post on why agents ignore instructions walks through the usual reasons.
Ask for small diffs. "Change only the validate function" produces a patch. "Fix the form" often produces a rewritten file, and a rewritten file is how your edit gets lost.
Review the diff, not the summary. The agent's description of what it did is not what it did. Read the diff, as in reviewing an AI agent's git diff before merging.
Keep one writer per folder. If two tools or two agent sessions touch the same working copy, the later write wins. See two AI coding agents on the same repo for ways to separate them.
When the agent reverts a test on purpose
One case deserves its own note. If you change a test to describe the behaviour you want and the agent changes it back, it is trying to make the suite green by bending the test to the code. That is a known pattern, covered in why an AI coding agent keeps rewriting your tests. The fix is the same shape: state the rule in writing, such as "never edit existing tests without asking", and review test diffs first.
For the wider picture on working with coding agents, see our guide to AI coding tools.
Frequently asked questions
Why does my AI coding agent undo my edits?
Usually because it is working from a stale copy of the file and writes the whole file back, or because a formatter or generator rewrites your change. Less often it treats your edit as a bug.
How do I get back changes an AI agent overwrote?
If the work was committed, use git reflog to find the earlier state and git restore --source=<commit> <path> to bring a file back. Check git stash list too. Uncommitted edits that were overwritten may only survive in your editor's local history.
Can I stop an AI agent from touching certain files?
You can ask in the instructions file and make the ask specific, but that is a request, not a lock. For hard protection, keep those files outside the agent's working folder or review diffs before you accept them.
Should I commit before every agent change?
Yes, it is the cheapest safeguard. A checkpoint commit before each turn means any bad edit can be undone with one command, and you can clean up the history before merging.
Does this happen with every AI coding tool?
The mechanisms are not specific to one tool. Stale context, whole-file rewrites, generators and shared working folders appear anywhere an agent edits files you also edit.
How did this land?
About the author

Staff Engineer, Platform
Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.


