PostToolUse: EnterWorktree hook does not fire when worktree is created via `claude -w`

Resolved 💬 3 comments Opened Mar 26, 2026 by japaz Closed Apr 24, 2026

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: EnterWorktree hook 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.env causes make refresh-env-files to fail)

The user only discovers the problem when they try to run commands inside the session.

Expected behaviour

One of:

  1. claude -w fires the PostToolUse: EnterWorktree hook after creating and entering the worktree, consistent with what happens when the EnterWorktree tool is used internally.
  2. A dedicated WorktreeCreated (or SessionStart) 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: EnterWorktree hook configured in project settings.local.json
  • macOS, zsh

View original on GitHub ↗

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