Git worktrees resolve to main worktree's memory directory instead of their own
Description
When Claude Code is launched from a manually created git worktree, the auto memory directory resolves to the main worktree's project path instead of the linked worktree's path.
Reproduction
# Setup: Monocle-a is the main worktree, Monocle-b is a linked worktree
git worktree list
# /Users/george/Code/Monocle-a 742e462 [main] ← main worktree
# /Users/george/Code/Monocle-b 742e462 [Monocle-b] ← linked worktree
Launch Claude Code from the linked worktree:
cd /Users/george/Code/Monocle-b
claude
Expected: Memory directory = ~/.claude/projects/-Users-george-Code-Monocle-b/memory/
Actual: Memory directory = ~/.claude/projects/-Users-george-Code-Monocle-a/memory/
The system prompt contains:
You have a persistent auto memory directory at \/Users/george/.claude/projects/-Users-george-Code-Monocle-a/memory/\
Even though CWD is /Users/george/Code/Monocle-b and git rev-parse --show-toplevel returns /Users/george/Code/Monocle-b.
Root cause
Claude Code appears to use git rev-parse --git-common-dir (or equivalent) to derive the project path, which returns the main worktree's .git directory for all linked worktrees:
cd /Users/george/Code/Monocle-b
git rev-parse --git-common-dir
# /Users/george/Code/Monocle-a/.git
This causes both worktrees to map to the same project directory (-Users-george-Code-Monocle-a), even though ~/.claude/projects/-Users-george-Code-Monocle-b/memory/MEMORY.md exists with correct Monocle-b-specific content that never gets loaded.
Impact
- Memory written for the linked worktree goes to the wrong project directory (the main worktree's)
- The correct memory file at
-Users-george-Code-Monocle-b/memory/is never loaded - Two independent stacks (different branches, different deployments, different VIPs) share a single memory file, causing cross-contamination
- This is distinct from #24382 which discusses CWD-based path derivation — this bug is specifically about git common dir resolution overriding the CWD path
Environment
- Claude Code: 2.1.77 (@anthropic-ai/claude-code)
- macOS Darwin 25.4.0 (arm64)
- Git worktree created manually via
git worktree add
Related
- #24382 (closed as completed, but the underlying worktree resolution issue persists for manually created worktrees)
- #16600, #15776
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗