claude-code - 💡(How to fix) Fix Subagent commits silently delete unrelated files via broad git staging [4 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#45108Fetched 2026-04-09 08:13:05
View on GitHub
Comments
4
Participants
3
Timeline
11
Reactions
0
Author
Timeline (top)
labeled ×5commented ×4cross-referenced ×2

Claude Code subagents (spawned via the Agent tool with isolation: "worktree") delete unrelated files when committing their work. The agent's commits include deletions of files the agent never touched, suggesting broad git staging (git add -A or git add .) despite the system prompt instructing "prefer adding specific files by name."

This has happened 3 times in our project over 2 days, deleting production source code — not just documentation.

Update: Initial report stated commits were "direct on master." Forensic reflog analysis revealed they were worktree branch commits fast-forward merged onto master. This potentially links to #44965. See comments for full correction.

Error Message

  • Silent data loss — no warning or error when the agent commits the deletions. Only discovered during milestone audit.
  1. Hard enforcement: Claude Code's commit tooling should refuse or warn when a commit includes file deletions that weren't explicitly requested

Root Cause

Root Cause Hypothesis

Fix Action

Workaround

Manually verifying commits via git show --stat after every subagent execution and maintaining restore scripts. This is fragile and labor-intensive.

RAW_BUFFERClick to expand / collapse

Summary

Claude Code subagents (spawned via the Agent tool with isolation: "worktree") delete unrelated files when committing their work. The agent's commits include deletions of files the agent never touched, suggesting broad git staging (git add -A or git add .) despite the system prompt instructing "prefer adding specific files by name."

This has happened 3 times in our project over 2 days, deleting production source code — not just documentation.

Update: Initial report stated commits were "direct on master." Forensic reflog analysis revealed they were worktree branch commits fast-forward merged onto master. This potentially links to #44965. See comments for full correction.

Reproduction

Environment

  • Claude Code CLI v2.1.92
  • Claude Opus 4.6 (1M context)
  • Linux (Proxmox LXC)
  • Git repository with .planning/phases/ directories containing per-phase artifacts
  • Subagents spawned with isolation: "worktree" (via GSD framework default)

Steps

  1. Have a repository with multiple "phase" directories containing planning docs and source code
  2. Use Claude Code to spawn an executor subagent (via Agent tool with isolation: "worktree") to work on Phase N+2
  3. The subagent makes commits for its assigned work (e.g., adding test files)
  4. Observe: The subagent's commits also delete files from Phase N and Phase N+1 directories, plus production source code created by those phases

What happens

The subagent's commits include:

  • The intended changes (test files, implementation)
  • Unintended deletions of 20-30+ files from other phases
  • Unintended modifications to shared files (ROADMAP.md, REQUIREMENTS.md, STATE.md) reverting them to older state

Expected behavior

The subagent should only stage and commit files it explicitly created or modified.

Evidence

Incident 1: Phase N+1 executor deletes Phase N files

  • Intended: Add 3 test files
  • Actually did: Deleted 22 files including:
    • A 406-line production module
    • An 883-line test suite
    • Several utility scripts
    • 8 Phase N planning files, 6 Phase N+1 planning files
    • Modified a 7000+ line orchestrator file removing ~149 lines of Phase N wiring
  • Verified: Parent commit confirmed to have all deleted files via git ls-tree
  • Delivery: Worktree branch fast-forward merged onto master (initially misreported as direct commit)

Incident 2: Phase N+2 executor deletes Phase N and N+1 files (again)

  • Intended: Add 3 test files for Phase N+2
  • Actually did: Deleted 34 files including all Phase N, N+1, and N+2 planning files
  • Same pattern: worktree branch, broad staging

Incident 3: Partial restoration also deleted

  • A restore commit after Incident 2 fixed some files but left production source missing until manual forensic restoration

Forensic verification: No external automation

We thoroughly verified no system-level automation is responsible:

  • No active git hooks (only .sample files in .git/hooks/)
  • Claude Code hooks (.claude/hooks/) are advisory-only (read-only, never modify git state)
  • No pre-commit framework
  • Empty crontab
  • No Syncthing, rsync, or file sync tools
  • No IDE auto-staging or auto-commit configuration
  • No background git processes

Impact

  • Production source code deleted (not just documentation) — 406-line module, 883-line test suite, query scripts
  • Orchestrator wiring reverted — phase integration code removed from main 7000+ line file
  • Config reverted — YAML config sections removed
  • 3 incidents in 2 days — recurring and escalating (each incident deletes more files)
  • Manual forensic restoration required — reverse-patching via git diff <commit>..<commit>^ | git apply
  • Silent data loss — no warning or error when the agent commits the deletions. Only discovered during milestone audit.

Root Cause Hypothesis

Updated: Two possible root causes, potentially compounding:

  1. Worktree branch point (#44965): EnterWorktree may create the worktree from main (or another ref) instead of current HEAD. Files committed after that branch point are absent from the worktree. When the agent stages with git add -A, these absent files become explicit deletions.

  2. Broad staging (original hypothesis): Even with a correct branch point, the subagent uses broad git staging (git add -A, git add .) rather than staging only files it created/modified via Edit/Write tools. Any file absent from the working tree — for any reason — gets staged as a deletion.

The system prompt correctly instructs: "prefer adding specific files by name rather than using git add -A or git add ., which can accidentally include sensitive files or large binaries" — but the agent does not reliably follow this instruction.

Suggested Fix

  1. Hard enforcement: Claude Code's commit tooling should refuse or warn when a commit includes file deletions that weren't explicitly requested
  2. Staging guard: Before committing, check git diff --cached --diff-filter=D and require explicit confirmation for any deletions
  3. Subagent scope restriction: Subagents should only be able to stage files within their assigned scope (e.g., files they created or modified via Edit/Write tools)
  4. Worktree fix (#44965): Ensure EnterWorktree branches from current HEAD, not main

Workaround

Manually verifying commits via git show --stat after every subagent execution and maintaining restore scripts. This is fragile and labor-intensive.

extent analysis

TL;DR

The most likely fix involves modifying Claude Code's commit tooling to enforce specific file staging and prevent unintended deletions by subagents.

Guidance

  • Implement a staging guard to check for unintended deletions before committing, using git diff --cached --diff-filter=D to identify deleted files.
  • Restrict subagent scope to only stage files within their assigned scope, i.e., files they created or modified via Edit/Write tools.
  • Verify commits manually using git show --stat after every subagent execution to detect potential issues.
  • Consider addressing the potential root cause related to EnterWorktree branching from main instead of current HEAD, as described in #44965.

Example

A possible implementation of the staging guard could involve adding a pre-commit hook that checks for deleted files using git diff --cached --diff-filter=D and prompts for confirmation before proceeding with the commit.

Notes

The provided workaround of manually verifying commits and maintaining restore scripts is labor-intensive and fragile. A more robust solution involves modifying Claude Code's commit tooling to prevent unintended deletions.

Recommendation

Apply the suggested fix by implementing a staging guard and restricting subagent scope to prevent unintended deletions, as this addresses the root cause of the issue and provides a more robust solution than the current workaround.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

The subagent should only stage and commit files it explicitly created or modified.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING