[BUG] CLAUDE.md and .claude/rules/ loaded twice in git worktrees
Description
When running Claude Code inside a git worktree, project instructions (CLAUDE.md, AGENTS.md, .claude/rules/*.md) are loaded twice — once from the worktree path and once from the main repository path. Both copies appear in the system context, doubling the context usage for project instructions.
Reproduction steps
- Have a repo with
CLAUDE.mdand/or.claude/rules/containing project instructions - Create a git worktree:
``bash``
git worktree add .worktrees/my-feature -b my-feature
- Open Claude Code in the worktree directory:
``bash``
cd .worktrees/my-feature
claude
- Observe the loaded context — every project instruction file appears twice:
Contents of /path/to/main-repo/CLAUDE.mdContents of /path/to/main-repo/.worktrees/my-feature/CLAUDE.mdContents of /path/to/main-repo/.claude/rules/frontend.mdContents of /path/to/main-repo/.worktrees/my-feature/.claude/rules/frontend.md
Expected behavior
Each project instruction file should be loaded exactly once, regardless of whether running in a worktree or the main repo.
Actual behavior
Every file from CLAUDE.md, AGENTS.md, and .claude/rules/ is loaded twice. Both copies contain identical content since they are the same files in the same git repository.
Root cause
Same as #26992. In a git worktree, .git is a file pointing back to the main repo:
gitdir: /path/to/main-repo/.git/worktrees/<name>
Claude Code discovers project instructions from both:
- The worktree's CWD (e.g.,
.worktrees/my-feature/CLAUDE.md) - The main repository root (by following
.gitback to the main repo, e.g.,main-repo/CLAUDE.md)
Since both paths resolve to the same git content, every instruction file appears twice.
Suggested fix
- Deduplicate discovered files by inode or content hash
- Or resolve both paths to the same canonical git root and only load once
- Or detect worktree context and only load from the CWD path
Environment
- Claude Code version: 2.1.47
- OS: macOS (Darwin 25.0.0)
- Shell: zsh
Related issues
- #26992 — Same root cause, but for duplicate slash commands in worktrees
- #27069 — Duplicate skills/commands in worktrees
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗