Claude Code Overwrites Existing Changes

Uncommitted edits gone after agent ran. Local-history recovery, then enforce "commit before agent" + dirty-state checks.

You had two hours of uncommitted edits in src/billing.ts — debug logging, a half-finished refactor, a TODO comment with your reasoning. You ran Claude Code on a “quick fix” task. The diff replaces the whole file. Your edits are gone from the working tree, and you didn’t commit them.

This is recoverable more often than it feels. Editors keep local history, git keeps the reflog, and even fully-overwritten files often have copies in editor buffers, swap files, or backup snapshots. Below: how to recover in the next 10 minutes, plus the four-layer “this won’t happen again” setup.

Common causes

Ordered by hit rate, highest first.

1. Working tree was dirty when agent started

Agent’s Read of the file captured your dirty state. Its subsequent Write replaced the whole file with its version of the edits — your uncommitted changes never made it into the diff because the agent didn’t preserve them.

How to spot it: Look at the agent’s Write tool calls. If it replaced the whole file (not a patch), your uncommitted edits were silently overwritten.

2. Multiple agents touched the same file in parallel

Two Claude Code sessions or one Claude + one Cursor edit the same file. Later writer wins; earlier writer’s changes vanish.

How to spot it: git log src/billing.ts shows nothing unusual, but the file content matches only one agent’s intent. The other agent’s work is gone.

3. Agent used a full-file write instead of an Edit/patch

For large changes, agents sometimes use full-file rewrites. Your inline debug logs sit inside the file Claude rewrote — they’re not preserved unless Claude saw them as part of the task.

How to spot it: Diff shows the entire file changed (every line touched) rather than localized hunks. That’s a full rewrite.

4. You closed the editor buffer thinking the file was saved

Your edits were in an unsaved editor buffer. Closing the editor or restarting it discarded the buffer; agent’s saved version is what’s on disk.

How to spot it: You don’t remember saving (Cmd+S) before the agent ran. The change is missing from disk but might be in editor recovery.

5. Agent committed a “checkpoint” that included its overwrite

Claude was prompted to commit after work. Its commit captured the post-overwrite state. Your edits never made it into git — but they may still be in editor local history.

How to spot it: git log -1 --stat shows a recent agent commit. Your edits aren’t in any commit. Editor history is the remaining hope.

6. Stash was applied wrong or popped on a different branch

You stashed your edits, switched branches for the agent task, and the stash got popped onto the wrong branch — or never popped at all.

How to spot it: git stash list shows stashes you didn’t realize were still pending, or your branch doesn’t have the changes you remember stashing.

Shortest path to fix

Ordered by urgency. Don’t close the editor until you’ve checked local history.

Step 1: Stop. Don’t close the editor.

Most recovery paths require the editor to still be running. If you close it (or restart), some buffer-based recoveries are lost.

- Do NOT close VS Code / Cursor / Sublime / your editor
- Do NOT run `git stash` or any other state-changing git command yet
- Do NOT run the agent again "to undo"

Lock in the current state until you’ve inventoried what’s recoverable.

Step 2: Check editor local history

Every modern editor keeps file history independent of git:

VS Code / Cursor / Windsurf:

Right-click on the file in Explorer → Open Timeline
Or: View → Timeline

You’ll see entries like “Saved 15 min ago” — click to diff/restore.

JetBrains (IntelliJ, WebStorm):

Right-click file → Local History → Show History

Sublime Text: install “Local History” package; otherwise no built-in.

This recovers 80% of “agent overwrote my edits” cases. Try this before anything else.

Step 3: Check unsaved buffer with Cmd+Z repeatedly

If the file is still open in the editor and you didn’t restart, the buffer’s undo history may have your edits:

1. Open the affected file in your editor
2. Click in the file (focus the buffer)
3. Cmd+Z (or Ctrl+Z) repeatedly — watch the diff revert toward your edits
4. Stop when you see your changes restored
5. Save (Cmd+S) immediately

If Cmd+Z jumps past your edits or undo history was cleared, fall back to local history.

Step 4: Check git reflog and stashes

Even if your edits weren’t formally committed, sometimes a checkpoint exists:

# Find lost commits (including by Claude Code's auto-commits)
git reflog --all | head -30

# Find any stashes
git stash list

# Look at editor swap/backup files (vim, emacs)
ls -la .*.sw[opq] 2>/dev/null
find . -name "*.orig" -o -name "*~" 2>/dev/null

If anything in the reflog has your edits, recover with:

git checkout <hash> -- src/billing.ts

Step 5: If truly lost, rewrite + commit the commit-before-agent rule

If you’ve checked all recovery paths and the edits are gone, rewrite them. Then institute the prevention:

# Pre-agent ritual
git status                             # confirm working tree
git add -A && git commit -m "wip"      # checkpoint everything
# now run the agent

A “wip” commit you’ll squash later is cheaper than 2 hours of lost work.

Step 6: For multi-agent or parallel work, use git worktrees

If you frequently run agents while editing in parallel:

# Create a worktree for agent work
git worktree add ../project-agent main

# Now: edit in `project/`, run agent in `project-agent/`
# Each has its own working tree — no overwrites

When the agent finishes, merge its branch back into your main worktree on your schedule.

Prevention

  • “Commit before agent” is a hard rule — never run an agent with a dirty working tree
  • For exploratory wip you don’t want to commit, use git stash (and verify after with git stash list)
  • Use git worktrees for parallel agent + manual editing — separate working trees can’t collide
  • In agent prompts, list files Claude must NOT touch: “do not edit src/billing.ts; it has uncommitted work”
  • Enable editor auto-save with a 1-second debounce so unsaved buffer states are rare
  • Make CLAUDE.md require Edit-style patches over full-file Writes when the change is localized

Tags: #Troubleshooting #Claude Code #Debug #Overwrite