claude-code - ✅(Solved) Fix Hooks not triggered when working in a git worktree [1 pull requests, 2 comments, 3 participants]

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…
GitHub stats
anthropics/claude-code#46808Fetched 2026-04-12 13:32:31
View on GitHub
Comments
2
Participants
3
Timeline
7
Reactions
0
Author
Timeline (top)
labeled ×3commented ×2cross-referenced ×2

Claude Code hooks defined in .claude/settings.json are not triggered when the active working directory is a git worktree (created via git worktree add).

Root Cause

In a git worktree, the .git entry is a file (not a directory) that points back to the main repo. The .claude/ directory lives in the main repo root, not in the worktree directory. Claude Code appears to resolve .claude/settings.json relative to the process working directory rather than using git rev-parse --show-toplevel to locate the repository root and find .claude/ from there.

Fix Action

Fixed

PR fix notes

PR #68: feat: bkit v2.1.2 — CC v2.1.98 compatibility (ENH-193, #46808)

Description (problem / solution / changelog)

Summary

  • Restore the MCP _meta.maxResultSizeChars 500K override path on Claude Code v2.1.98 by setting the key on both result._meta and content[0]._meta (ENH-193).
  • Detect linked git worktrees on session start and warn users that CC hooks may not fire reliably (anthropics/claude-code#46808).
  • Sync version strings to 2.1.2 across plugin manifests, marketplace metadata, hooks, and config.

What's in this PR

AreaFilesNotes
MCP serversservers/bkit-pdca-server/index.js, servers/bkit-analysis-server/index.jsENH-193 dual _meta keys
Worktree guardlib/core/worktree-detector.js (new), hooks/startup/context-init.js#46808 mitigation
Manifestsbkit.config.json, .claude-plugin/plugin.json, .claude-plugin/marketplace.json, hooks/hooks.jsonVersion → 2.1.2
DocsCHANGELOG.md, docs/01-plan/, docs/02-design/, docs/03-analysis/, docs/04-report/Full PDCA artifacts + QA matrix

Verification

  • npx jest --silent — 2 suites / 6 tests pass
  • claude -p --plugin-dir . --output-format json — 7/7 smoke tests, is_error: false, 0 retries
  • MCP servers — require() succeeds, E2E bkit_pdca_status / bkit_feature_list / bkit_checkpoint_list return valid payloads
  • Static — 38 skills / 36 agents / 21 hooks frontmatter validated; description ≤ 250 chars; effort field 100% coverage

Compatibility

  • Claude Code: v2.1.98 verified, 63 consecutive compatible releases (v2.1.34 → v2.1.98)
  • No breaking changes to bkit public API or PDCA state machine

Test plan

  • Unit tests pass
  • Headless smoke matrix pass (7/7)
  • MCP runtime verification
  • Frontmatter / version sync validation
  • CI green (will appear after push)

🤖 Generated with Claude Code

Changed files

  • .claude-plugin/marketplace.json (modified, +2/-2)
  • .claude-plugin/plugin.json (modified, +1/-1)
  • CHANGELOG.md (modified, +20/-0)
  • README.md (modified, +1/-1)
  • bkit.config.json (modified, +1/-1)
  • docs/01-plan/features/cc-v2196-v2197-impact-analysis.plan.md (added, +60/-0)
  • docs/01-plan/features/cc-v2197-v2198-impact-analysis.plan.md (added, +53/-0)
  • docs/01-plan/features/cc-version-issue-response.plan.md (added, +288/-0)
  • docs/02-design/features/cc-version-issue-response.design.md (added, +216/-0)
  • docs/03-analysis/features/bkit-v211-comprehensive-qa-agents.md (added, +445/-0)
  • docs/03-analysis/features/bkit-v211-comprehensive-qa-hooks-mcp.md (added, +271/-0)
  • docs/03-analysis/features/bkit-v211-comprehensive-qa-lib-tests.md (added, +247/-0)
  • docs/03-analysis/features/bkit-v211-comprehensive-qa-skills.md (added, +244/-0)
  • docs/03-analysis/features/cc-version-issue-response.iterate.md (added, +31/-0)
  • docs/03-analysis/features/cc-version-issue-response.simplify.md (added, +97/-0)
  • docs/04-report/features/bkit-v200-test.report.md (modified, +14/-29)
  • docs/04-report/features/cc-v2196-v2197-impact-analysis.report.md (added, +292/-0)
  • docs/04-report/features/cc-v2197-v2198-impact-analysis.report.md (added, +298/-0)
  • docs/04-report/features/cc-version-issue-response.full-qa.md (added, +177/-0)
  • docs/04-report/features/cc-version-issue-response.qa.md (added, +80/-0)
  • docs/04-report/features/cc-version-issue-response.report.md (added, +87/-0)
  • hooks/hooks.json (modified, +1/-1)
  • hooks/session-start.js (modified, +1/-1)
  • hooks/startup/context-init.js (modified, +15/-0)
  • lib/core/worktree-detector.js (added, +84/-0)
  • servers/bkit-analysis-server/index.js (modified, +7/-1)
  • servers/bkit-pdca-server/index.js (modified, +7/-1)
RAW_BUFFERClick to expand / collapse

Summary

Claude Code hooks defined in .claude/settings.json are not triggered when the active working directory is a git worktree (created via git worktree add).

Environment

  • Claude Code CLI
  • Git worktree setup: main repo at /path/to/repo, worktree at /path/to/repo/worktrees/feature-branch

Steps to reproduce

  1. Set up a hook in .claude/settings.json at the main repo root, e.g. a PreToolUse hook on Bash that intercepts git commit
  2. Create a git worktree: git worktree add worktrees/my-feature -b my-feature
  3. Open a Claude Code session with the worktree as the working directory
  4. Run git commit from within the session

Expected: The PreToolUse hook fires and runs the configured command.

Actual: The hook is silently skipped. Claude Code does not find .claude/settings.json because it looks relative to the current working directory, not the git repository root.

Root cause

In a git worktree, the .git entry is a file (not a directory) that points back to the main repo. The .claude/ directory lives in the main repo root, not in the worktree directory. Claude Code appears to resolve .claude/settings.json relative to the process working directory rather than using git rev-parse --show-toplevel to locate the repository root and find .claude/ from there.

Impact

Any project that uses .claude/hooks/ for pre-commit checks, linters, or test runners will silently lose those guardrails whenever a developer works inside a git worktree. In our case, a PreToolUse hook runs cargo test inside Docker before every commit — this hook was bypassed in the worktree, causing tests to run directly on the host and inadvertently interact with the local tmux session.

Suggested fix

When resolving .claude/settings.json, fall back to git rev-parse --show-toplevel to find the repository root and look for .claude/ there, so hooks work consistently whether the session is started from the main worktree or any linked worktree.

extent analysis

TL;DR

Modify Claude Code to resolve .claude/settings.json using git rev-parse --show-toplevel to ensure hooks work in git worktrees.

Guidance

  • Identify the current working directory resolution logic in Claude Code and modify it to use git rev-parse --show-toplevel to find the repository root.
  • Update the .claude/settings.json loading mechanism to look for the file relative to the repository root, rather than the current working directory.
  • Verify the fix by creating a git worktree, setting up a hook in .claude/settings.json, and running the hook from within the worktree.
  • Consider adding a fallback or warning when .claude/settings.json is not found at the expected location to prevent silent failures.

Example

# Example of using git rev-parse to find the repository root
REPO_ROOT=$(git rev-parse --show-toplevel)
SETTINGS_PATH="$REPO_ROOT/.claude/settings.json"

Notes

This fix assumes that git rev-parse --show-toplevel is available and functional in the environment where Claude Code is running. Additionally, this solution may require modifications to the Claude Code CLI to accommodate the new resolution logic.

Recommendation

Apply workaround: Modify the Claude Code resolution logic to use git rev-parse --show-toplevel to ensure consistent hook behavior across main and linked worktrees. This approach addresses the root cause of the issue and provides a reliable solution for projects using git worktrees.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING