The AI rewrote my whole file when I asked for one small change
Any AI coding tool Last checked 2026-10-14
Regenerating the file is easier for the model than surgically patching it, so unless you constrain it, it rewrites — and quietly drops comments, edge cases and anything it judged unnecessary. Ask for a diff, not a file, and keep a clean git tree so you can always see exactly what it removed.
What you saw
You asked it to change a button colour or add one field. The diff is 300 lines. Formatting changed, comments vanished, an error-handling branch you needed is gone, and something unrelated broke.
Or: it "refactored while it was in there" — renamed things, extracted functions, swapped a library — none of which you asked for.
What it actually means
Generating a fresh, coherent file is the thing these models are best at. Producing a minimal patch is harder and more error-prone for them, so left to their own judgement many will rewrite.
When it regenerates, everything it does not reproduce is deleted. Code that looked redundant to a model with no knowledge of your production incidents — a null check, a workaround comment, a retry — gets cleaned away.
Agentic tools make this worse by rewarding visible progress. "I also tidied up the rest of the file" reads as helpfulness and lands as an unreviewed refactor.
The real risk is not the rewrite. It is that the rewrite is too large to review properly, so you skim it, accept it, and inherit the deletions.
The fix
Revert first, then re-ask. Do not patch a bad rewrite with another prompt — you will be reviewing a diff against code you never approved.
git checkout -- path/to/file.tsx
Constrain the edit explicitly in the request:
Change only the colour of the submit button in Checkout.tsx.
Do not reformat, rename, refactor, or remove comments.
Show me the minimal diff and nothing else.
Ask for the diff before the write when the file matters:
Show the exact lines you intend to change, before and after. Do not edit the file until I say go.
Review the diff by what was removed, not what was added. Deletions are where the damage is.
git diff --stat
git diff -- path/to/file.tsx
If the file is huge and keeps getting rewritten wholesale, that is a signal in itself — split it. Smaller files produce smaller, reviewable edits.
How to stop it happening again
Commit before every AI task. A clean tree turns "what did it change?" into one command. This is the single highest-value habit in vibe coding.
Put a standing rule in your project rules / CLAUDE.md: minimal diffs only, no unrequested refactors, no reformatting, never delete comments or error handling without asking.
Turn on format-on-save with a committed formatter config, so formatting is never part of the model's diff and never hides a real change.
Write tests for the behaviour you care about. A rewrite that drops an edge case is invisible in review and loud in a test run.
Keep files small enough to read in one screen or two. Model edit quality drops sharply with file size.
Related questions
How do I see exactly what it deleted?
git diff shows removals as minus lines. Read those first. If you did not commit beforehand, most editors keep a local history — in VS Code and Cursor, right-click the file and look for Local History or Timeline.
Why does it reformat everything?
It regenerates the file in its own default style. A committed formatter config plus format-on-save makes style a non-event so the diff only shows real changes.
Is asking for a diff actually more reliable than asking for the file?
Yes, for review purposes. It forces the model to state its intent in a small, checkable form before anything touches disk, and you can reject it for free.