claude-code - 💡(How to fix) Fix Worktree feature is subdirectory-blind in monorepos (loses subdirectory-scoped CLAUDE.md)

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…

When a Claude Code session is started from a subdirectory of a git repo and the worktree feature is invoked, the resulting worktree session loads the repo-root CLAUDE.md instead of the subdirectory's CLAUDE.md. This breaks the "subdirectory-as-agent-identity" pattern used in monorepos.

Root Cause

When a Claude Code session is started from a subdirectory of a git repo and the worktree feature is invoked, the resulting worktree session loads the repo-root CLAUDE.md instead of the subdirectory's CLAUDE.md. This breaks the "subdirectory-as-agent-identity" pattern used in monorepos.

Fix Action

Workaround

Manually cd into the equivalent subdirectory inside the worktree and start a new session there.


Generated by AI (Claude)

Code Example

/MyRepo
     .git/
     CLAUDE.md                ← orchestrator identity
     AgentA/
       CLAUDE.mdAgentA identity
     AgentB/
       CLAUDE.mdAgentB identity
RAW_BUFFERClick to expand / collapse

Summary

When a Claude Code session is started from a subdirectory of a git repo and the worktree feature is invoked, the resulting worktree session loads the repo-root CLAUDE.md instead of the subdirectory's CLAUDE.md. This breaks the "subdirectory-as-agent-identity" pattern used in monorepos.

Repro

  1. Have a monorepo where subdirectories define distinct agent identities via per-folder CLAUDE.md. Example layout:
    /MyRepo
      .git/
      CLAUDE.md                ← orchestrator identity
      AgentA/
        CLAUDE.md              ← AgentA identity
      AgentB/
        CLAUDE.md              ← AgentB identity
  2. Start a Claude Code session with cwd = /MyRepo/AgentA/. Confirm session loads AgentA's CLAUDE.md.
  3. From that session, create a worktree (Claude Code worktree feature).
  4. The worktree is created at /MyRepo/AgentA/.claude/worktrees/<name>/, but its .git pointer resolves to the parent repo (/MyRepo/.git/worktrees/<name>), so the worktree's working tree is the full repo (with CLAUDE.md, AgentA/, AgentB/, etc. as direct children).
  5. The worktree session opens at the worktree root → loads root CLAUDE.md, not AgentA/CLAUDE.md.

Why this happens

git worktree add always creates a working tree of the entire repo — there is no subdirectory-scoped worktree in git. Claude Code's worktree feature uses git worktrees under the hood, then starts the session at the worktree root, which discards the original cwd's subdirectory position.

Expected behavior

When a session is invoked from subdirectory <S> of repo <R> and creates a worktree, the new worktree session should start with cwd = <worktree-root>/<S>, not <worktree-root>. That preserves the agent identity defined by the per-subdirectory CLAUDE.md.

Impact

For monorepos that use per-folder CLAUDE.md to scope agent identity (a common multi-agent pattern), the worktree feature silently swaps the agent under you. You think you're running AgentA, you're actually running the orchestrator. We discovered this only when the agent answered "who am I?" with the wrong identity mid-task.

Suggested fix

Record the subdirectory offset between the session cwd and the enclosing repo root at worktree-creation time. When starting the worktree session, cd into <worktree-root>/<offset> before loading instructions.

Workaround

Manually cd into the equivalent subdirectory inside the worktree and start a new session there.


Generated by AI (Claude)

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

When a session is invoked from subdirectory <S> of repo <R> and creates a worktree, the new worktree session should start with cwd = <worktree-root>/<S>, not <worktree-root>. That preserves the agent identity defined by the per-subdirectory CLAUDE.md.

Still need to ship something?

×6

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

Back to top recommendations

TRENDING