[BUG] Auto-memory resolves to main repo path for git worktrees, preventing per-worktree memory

Resolved 💬 3 comments Opened Mar 4, 2026 by kalaspace Closed May 25, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

When running Claude Code inside a git worktree, the auto-memory directory resolves to the main repository's filesystem path instead of the worktree's path. This means all worktrees of the same repo share a single MEMORY.md, which may not be desirable when worktrees have diverged significantly (e.g. long-lived feature branches with different architectural decisions).

Root cause

The project identity resolution follows the .git file → gitdir: pointer → --git-common-dir to find the main repo root, then slugifies that path for the memory directory name.

In a worktree:

  • .git is a file containing gitdir: /path/to/backend/.git/worktrees/backendC2A
  • git rev-parse --git-common-dir returns /path/to/backend/.git
  • Claude Code derives the project path as /path/to/backend → -path-to-backend

Meanwhile, session data (.jsonl files) is correctly stored under the worktree-specific project directory (-path-to-backendC2A), creating an inconsistency.

What Should Happen?

Either:

  1. Use git rev-parse --show-toplevel (which correctly returns the worktree path) instead of resolving through --git-common-dir
  2. Provide a config option to opt into per-worktree memory
  3. Store memory under the same directory as sessions (worktree-specific)

Error Messages/Logs

Steps to Reproduce

  1. Have a repo at /path/to/backend
  2. Create a worktree: git worktree add ../backendC2A some-branch
  3. Run claude from /path/to/backendC2A
  4. Observe the system prompt says the auto-memory directory is ~/.claude/projects/-path-to-backend/memory/ (main repo path) instead of -path-to-backendC2A/memory/

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.66

Platform

Anthropic API

Operating System

Ubuntu/Debian Linux

Terminal/Shell

Other

Additional Information

_No response_

View original on GitHub ↗

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