← ClaudeAtlas

subagent-bash-cd-wrong-worktreelisted

Diagnose and prevent subagents committing to the wrong git branch when dispatched in multi-worktree setups. Use when (1) you dispatched a subagent (Task tool) with bash steps like `cd <target-worktree> && git commit ...`, (2) the subagent reported DONE with a real-looking commit SHA, (3) `git log` on the target worktree doesn't show that SHA — but `git branch --contains <sha>` finds it on a sibling branch (typically the parent repo's checked-out branch). Root cause: each Bash tool call starts in a shell whose cwd is reset to the agent's launch directory; `cd` in one call does not persist to the next. The subagent silently runs `git commit` from the launch directory's checked-out branch, not the worktree it intended.
wan-huiyan/agent-traffic-control · ★ 2 · AI & Automation · score 79
Install: claude install-skill wan-huiyan/agent-traffic-control
# Subagent + bash `cd` + multi-worktree → commit lands on wrong branch ## Problem You dispatched a subagent (via the Task tool) to do work in a specific git worktree. You wrote bash steps like: ```bash cd /path/to/.claude/worktrees/feature-X pip install -r requirements.txt git add foo.py git commit -m "..." ``` The subagent reports DONE with a commit SHA. The SHA is real (`git rev-parse <sha>` resolves) but `git log` on `feature-X`'s branch does not show it. `git branch --contains <sha>` reveals the commit landed on whatever branch was checked out in the **parent repository directory** (not your target worktree). **Why subagents miss this:** they trust their own `cd` step to have worked, then call `git commit` and see a successful commit message echoed by Git. Git commits the staged change to whatever HEAD is in the cwd Git resolves, which (after the shell reset) is the launch directory's HEAD — not the worktree's. ## Trigger Conditions - Multi-worktree git setup (`git worktree list` shows ≥ 2 entries) - Subagent dispatch (general-purpose Task tool, any model) - Subagent prompt instructed `cd <abspath> && <commands>` for shell steps - Subagent reported DONE with a SHA that doesn't appear in `git log` on the target worktree's branch - Subagent's first bash call shows the `cd` worked; subsequent calls silently ran in a different cwd ## Root Cause Each Bash tool call starts a fresh shell. Working directory persists *within* a single tool call (chained with `&&` or `