PostToolUse: EnterWorktree hook does not fire when worktree is created via `claude -w`
Summary
When using claude -w (or --worktree) from the CLI to start a session in a new worktree, the PostToolUse: EnterWorktree hook configured in settings.json is never triggered. This makes it impossible to run automated worktree setup (copying secrets, generating .env files, symlinking IDE config, etc.) reliably via hooks.
Background
Claude Code supports a PostToolUse hook with matcher EnterWorktree that fires when Claude uses the EnterWorktree tool internally — for example when an agent spawned with isolation: "worktree" enters its worktree. This hook is well-suited for worktree setup automation.
Example config in settings.local.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "EnterWorktree",
"hooks": [
{
"type": "command",
"command": "bash $CLAUDE_PROJECT_DIR/.claude/hooks.local/setup-worktree.sh"
}
]
}
]
}
}
Problem
When the user runs claude -w from the terminal, Claude Code creates a new git worktree and starts an interactive session in it directly — without going through the EnterWorktree tool. As a result:
- The
PostToolUse: EnterWorktreehook never fires - Worktree setup (copying
secrets.env, generating.env, setting up IDE symlinks, etc.) is silently skipped - The session starts in a broken state (e.g. missing
secrets.envcausesmake refresh-env-filesto fail)
The user only discovers the problem when they try to run commands inside the session.
Expected behaviour
One of:
claude -wfires thePostToolUse: EnterWorktreehook after creating and entering the worktree, consistent with what happens when theEnterWorktreetool is used internally.- A dedicated
WorktreeCreated(orSessionStart) hook event that fires whenever a new worktree session begins, regardless of how it was initiated.
Option 1 feels most natural given the existing hook model. Option 2 would be more general and also useful for other session-start setup needs.
Workaround
The current workaround is a git post-checkout hook that detects new worktree checkouts and runs setup. This works but is a separate mechanism that duplicates the intent of the Claude Code hook.
Environment
- Claude Code CLI (
claude -w) PostToolUse: EnterWorktreehook configured in projectsettings.local.json- macOS, zsh
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗