Worktree feature is subdirectory-blind in monorepos (loses subdirectory-scoped CLAUDE.md)

Resolved 💬 4 comments Opened May 20, 2026 by ohadw Closed Jun 22, 2026

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
``

  1. Start a Claude Code session with cwd = /MyRepo/AgentA/. Confirm session loads AgentA's CLAUDE.md.
  2. From that session, create a worktree (Claude Code worktree feature).
  3. 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).
  4. 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)

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗