Worktree

From melodic-software/claude-code-plugins · View interactive version

Worktree is an AI agent skill from the GitHub repository melodic-software/claude-code-plugins. When the built-in EnterWorktree or ExitWorktree tool resolves in this session, prefer ExitWorktree with action keep to leave a worktree; this skill to create one, to enter an existing one (claim check, then EnterWorktree by path), and to inventory, clean up, or audit worktrees. Manage git worktree l

How to use this skill

Skill file

Arguments. [create|status|cleanup|audit] [args]. e.g., /worktree create feat/my-feature, /worktree status, /worktree cleanup [<path>...] [--dry-run], /worktree audit

Repository context. Gather first

<!-- BEGIN GENERATED: gather-block. Edit docs/conventions/gather-block/block.md, then run scripts/sync-gather-block.py -->

Run each command below as its own Bash call, all sent together in one message (worktree isolation).

Keep any bound in the command's own pipe (output limits).

Unless this section says otherwise, treat a failure (not a repository, git unavailable) as an unknown value and carry on. Keep these as body calls, never ## Pre-computed context lines (gather-block convention).

<!-- END GENERATED: gather-block -->

git rev-parse --git-common-dir

Body calls keep this skill invocable from inside a worktree. The Bash shapes an isolated session

accepts at body time and in subagents are in reference/gather-block.md,

"Accepted command shapes for body-time and subagent Bash".

The refusal that record names is documented isolation enforcement, so the constraint is durable. Our rules: treat a

refused compound command as unverifiable, not as proof it would have reached the main checkout;

rewrite it as plain, separate commands; expect a delegated worker to inherit the same refusal; and

never use PowerShell, whose coverage is narrower, as a route around the git-redirect or

command-shape check. Pointer:

How Claude Code enforces isolation,

which lists the checks, their scope over subagents and background runs, and PowerShell's coverage.

Purpose

Orchestrate git worktree lifecycle from creation through cleanup. Front-half of the development workflow: it gets you into a worktree and keeps worktrees healthy. /source-control:pull-request is the back-half: prep, PR creation, monitoring, merge.

Why this exists: worktrees isolate parallel code changes, so Claude Code sessions on different tasks don't step on each other. Where branch protection blocks direct commits to main, every feature, fix, or refactor starts with a worktree or branch; this skill makes that seamless.

This skill canonically owns the parallel-session worktree convention for this plugin fleet, and reference/nesting-invariant.md § The nesting invariant, dated measurement is the one site that states the mechanism; every other surface in this plugin points there. That is an ownership claim, not a census: consumer docs outside this repository also describe worktree placement, at least one written after that file's as-of date. So ownership comes with an inbound channel: a consumer that measures something contradicting that file should open an issue on this plugin's tracker so the owner is corrected here. Without that back-channel the owner is the last to know.

Worktrees live at an external worktree_root (<root>/<owner>-<repo>-<slug>, outside every repository), defaulting to <plugin-data-dir>/worktrees when the key is unset, and never nested inside any repository's tree. Creation enforces that nesting invariant on the dated measured basis recorded below, not as a standing absolute. Keeping worktree_root clear of repository-discovery roots (such as a ghq root) is convention, not machine-checked: creation rejects only paths inside an existing repository, so a root inside a discovery tree still pollutes repository enumeration (ghq list reports each worktree as a repository, and a leading dot does not hide it). Choose a configured root accordingly. The default is already clear: the plugin data directory is harness state, never a checkout and never inside a discovery tree, which a checkout-relative default would be under a layout like <root>/github.com/<owner>/<repo>.

Adapting to your environment (graceful degrade)

This skill is self-contained: every action runs on plain git, plus the forge CLI for PR cross-referencing where available (GitHub provider reference). An adjacent capability it mentions (an issue tracker, a build/lint verifier, a session-start setup hook) is optional: use it when present, proceed without it otherwise. Project conventions (branch naming, worktree layout, which gitignored files a fresh worktree needs) come from the consuming project's own CLAUDE.md, rules, and hooks; read them before creating or removing anything.

Arguments

$ARGUMENTS. Action selector: parse the first token as the action, the rest as arguments.

| Action | Entry point | Use case |

|--------|-------------|----------|

| (empty) | Smart default | Detect current state, suggest appropriate action |

| create [name] | Create worktree | Validate name, explain setup, call EnterWorktree |

| status | Inventory | List all worktrees with PR status and staleness |

| cleanup [<path>...] [--dry-run] | Remove stale | Prune orphans, detect merged PRs and stale locks, remove with confirmation; named paths limit it to exactly those worktrees |

| audit | Health check | Run status + verify configuration health |


Action: Smart Default (empty args)

Detect current state and guide user to the right action.

  1. Check git repo: git rev-parse --is-inside-work-tree. If not in a repo → "Not in a git repository. status, cleanup and audit still work here, across repositories."
  1. Detect current branch: git rev-parse --abbrev-ref HEAD
  1. Check if in a worktree: git rev-parse --git-dir differs from git rev-parse --git-common-dir in any linked worktree. Common layouts: a .worktrees/ directory sibling to the repo's .claude/, Claude Code's default .claude/worktrees/, or a bare-clone hub (git rev-parse --git-common-dir ends in .bare and worktrees are siblings of .bare/).
  1. Branch-based guidance:
  1. Check for stale/prunable worktrees: Run bash "${CLAUDE_PLUGIN_ROOT}/scripts/lib/worktree-facts.sh" list <repo> and read its prunable column. If any worktrees are prunable or branches have merged PRs → suggest /source-control:worktree cleanup.
  1. Otherwise → Show brief status summary (worktree count, any needing attention).

Action: `create [name]`

Create a worktree with guided naming and setup verification. Full procedure (pre-flight guards for already-in-worktree and mid-session transition, name validation against EnterWorktree schema constraints, base-ref notes, the explain-before-create block, directory-rename caveats, post-create setup checks): context/create.md.

Safety invariants create must honor (full detail in context/create.md):

Orchestrated (autonomous) provisioning does not use this action. An orchestrator that must stay resident to keep dispatching (e.g. /work-items:work (if enabled)) cannot invoke create: the EnterWorktree terminal above would transition its own session and end its ability to orchestrate. It provisions non-interactively: the dispatched worker runs the shared worktree-create.sh helper directly (its output contract prints the path; the caller omits the EnterWorktree step), or a plain git worktree add followed by scripts/worktree-claim.sh claim <path> (the PostToolUse hook does this for Bash-tool adds, claiming only the parsed target), then runs the Workspace environment setup for it exactly as context/create.md § Workspace environment setup describes, then works the worktree via git -C <path> without entering it. When that direct invocation runs from outside the repository, add --repo-dir <repo-toplevel> to the flags the interactive path already passes (--name, --fallback-root-file, --data-root-file; see context/create.md). --repo-dir alone is incomplete: with worktreeroot.path unset, the helper gets the plugin-data default only through --data-root-file and otherwise exits 3. Also pass --base-ref head when the effective worktree.baseRef setting is head, resolved one settings layer per call as context/create.md § Base branch orders it; an omitted --base-ref defaults to fresh. The dispatching orchestrator (/work-items:work (if enabled)) owns that worker-side lifecycle end to end.

Pass this session's id on every direct helper invocation. ${CLAUDE_SESSION_ID} expands in this file. The helper's lock is this session's claim only when --session-id carries that resolved value, because the reason then contains session <id> since, the token check-enter matches. If the token is still literal, stop and do not call the helper: it treats an unexpanded value as a usage error and creates nothing. context/create.md gets the resolved value, not the token, because a context file is read as raw bytes.

Before writing in an existing worktree, run the claim gate. A helper-created tree already carries a lock reason; a plain git worktree add may not. Run it before git -C <path> writes, or before EnterWorktree(path:) into a tree this session did not just create:

bash "${CLAUDE_PLUGIN_ROOT}/scripts/worktree-claim.sh" check-enter <path> --session-id "${CLAUDE_SESSION_ID}"

${CLAUDE_SESSION_ID} is the skill-markdown substitution (same token as plugins/context-usage/reference/reader-contract.md). Pass it explicitly: the PostToolUse hook records the payload session_id in the lock. Without --session-id, the script falls back to CLAUDE_CODE_SESSION_ID from the Bash environment, then CLAUDE_SESSION_ID; with none of the three, check-enter cannot prove ownership and treats even this session's claim as foreign. If the literal ${CLAUDE_SESSION_ID} survives unexpanded, do not guess an id: surface that and stop.

Exit 4 prints FOREIGN CLAIM: <reason>: stop, another session holds a live claim. Exit 3 prints UNCLAIMED: claim it (claim <path> --session-id "${CLAUDE_SESSION_ID}") or leave it. Exit 0 is this session's claim, or a path that is not a linked worktree. report lists every linked worktree that still has no reason. The lock does not block writes; the reason is the claim other agents can read.

Resuming a handoff: hand the claim over before the gate. After a handoff and /clear the lock still names the old session, so check-enter reports it as foreign; first run the handover in context/create.md § Resuming a handoff.


Action: `status`

Inventory all worktrees with PR association, staleness detection, and a stranded-work axis. Collect Tier-0 facts with bash "${CLAUDE_PLUGIN_ROOT}/scripts/lib/worktree-facts.sh" list <repo> (do not parse porcelain by hand), a per-branch forge PR read, last-commit dates, and one run of ${CLAUDE_PLUGIN_ROOT}/scripts/landed-work.sh per repository. Then apply the two-axis classification, staleness threshold (14-day default; the configured override is ${user_config.worktree_stale_days}), the reap-age hours cleanup reads (48 default; ${user_config.worktree_reap_after_hours}), and presentation schema per context/status.md. audit Step 1 runs this logic internally. Outside a repository it covers every repository: context/status.md.

The Work axis answers what age and PR state cannot: whether removing a worktree would destroy a commit. It is classified first and outranks the rest, so a worktree holding unpushed unlanded commits is stranded, never merely stale. An unprovable verdict reports unknown and is treated exactly as stranded: the engine reports ? rather than no so an ambiguity is never read as safe.


Action: `cleanup [<path>...] [--dry-run]`

Safety invariants cleanup must honor (full detail in context/cleanup.md):

Remove stale worktrees, orphaned metadata, branches from merged PRs, and the project-scope plugin install records the worktree leaves behind. Full 5-step procedure: prune orphaned metadata → identify candidates (detection reasons: orphaned dir / prunable / PR-merged / stale / stale lock / reap age, the last proposing an already-safe worktree older than ${user_config.worktree_reap_after_hours} hours, 48 by default) → present → execute (4a guards → release file locks → down → reap records → remove, 4b branch deletion and current-worktree removal) → verify physical deletion: context/cleanup.md. --dry-run reports candidates and takes no action.

Scope. Paths given after cleanup limit the run to exactly those worktrees: the same classification and every Step 4 guard apply to each, and the repository-wide inventory and root scan are skipped. A bare cleanup in a session that created, entered or merged a PR from one worktree asks whether to target that worktree or the whole repository before inventorying; the whole-repository sweep behaves as before when chosen. See context/cleanup.md § Scope.


Action: `audit`

Periodic health check for worktree infrastructure, suitable as a recurring tracker work item. Step 1: run the status action internally, flagging any worktree whose Work axis is anything but safe (the closed mapping is in context/status.md; do not re-enumerate here) and any whose Status shows an issue (stale, merged-not-cleaned, prunable, locked, pr-unknown). Stranded work leads the findings: it is the only class where doing nothing is safer than acting. The Step 2 configuration-health checklist (delete_branch_on_merge, the worktreeroot.path conformance doctor, gitignored-file propagation), the Step 2b orphaned-plugin-install-record report, the Step 2c root scan of unregistered directories under the worktree root (${CLAUDE_PLUGIN_ROOT}/scripts/worktree-root-scan.sh; empty and a husk holding only its .git file go to cleanup, the other classes are reported only), and the Step 3 findings presentation: context/audit.md.

Step 2b reports; it never reaps. Records left by worktrees removed without the reap step (by hand, by another tool, or by an older cleanup) are out of that step's reach, so audit shows them in four buckets: live here, live elsewhere, candidate orphan, and other project records (information only, no remedy, because this plugin owns worktree lifecycle and nothing else). The live elsewhere bucket is load-bearing and is the one an implementation drops: one shared root at <root>/<owner>-<repo>-<slug> serves every repository, so "not in this repository's git worktree list" is true of every other repository's live worktree under it. Registration is scoped to one repository; a liveness test (git -C <path> rev-parse --is-inside-work-tree) is not, and both are required before anything is called an orphan. Removing an orphan needs its directory recreated first, a deliberate user act; the audit emits the commands and stops. Bare path non-resolution is exactly what an unmounted volume looks like, so a reap on it is never done.


Sandboxed sessions

When this session's Bash commands run in the sandbox, create and cleanup take the native-tool

route below. In a sandboxed session this section overrides the create invariants above and the

Routing and Mutation gate rules of the Boundary section below wherever they forbid EnterWorktree(name:) or

ExitWorktree(action: "remove"). Unsandboxed sessions keep the helper and EnterWorktree(path:)

route. Decide which route applies before running the helper. To tell whether this session's

commands run sandboxed, see

sandboxing: confirm commands run inside the sandbox.

root. For a new branch the hook uses the

helper's fresh base, with the remote default branch and fallback described in

Base branch. Resolve worktree.baseRef as described there

before calling the tool. If the session needs head, stop and report that the sandboxed route

cannot honor it. Follow the name validation and explain block in context/create.md,

describing the base and the steps for this route. Call EnterWorktree(name: "<validated name>"),

then run the Workspace environment setup through Bash from the worktree root. Report a failed

setup without retrying. For upstream behavior, read

WorktreeCreate,

what runs outside the sandbox,

and default placement.

Pointer: sandboxing: filesystem isolation,

for what a linked worktree may write in the shared .git.

Run cleanup's stranded-work and carried-file guards (both read-only), then the Workspace

environment down, then ExitWorktree(action: "remove"). The plugin's WorktreeRemove hook

handles the reap and removal, so skip cleanup's Bash reap. Read

WorktreeRemove for the tool's removal

arguments and Clean up worktrees

for native removal and branch handling. When the guard keeps the worktree and the push it offers

fails under the sandbox, leave with ExitWorktree(action: "keep") and report it. cleanup

reports every other candidate, and any git branch -D the sandbox refuses, for an unsandboxed

session to finish, as it does for a hook block.

Probe observation: the #17901 probe,

Claude Code 2.1.330 on Linux (WSL2), found Bash git worktree add and remove, and a commit run

from the main checkout into a worktree, all refused with the sandbox on, while the native create,

commit and remove route succeeded. As of: 2026-11-22. Recheck trigger: the sandboxing

page's filesystem-isolation section changes what a linked worktree may write in the shared .git,

or a Claude Code release changes the sandbox's filesystem rules or the worktree tools.

The nesting invariant, dated measurement

Worktrees stay outside every repository's tree. The measured basis, its disputed arms, the

verification stamp and its unconditional expiry are owned by

reference/nesting-invariant.md; read it before citing that

basis or changing the placement rule. A routine create follows the rule without reading it.

Boundary, the built-in `EnterWorktree` and `ExitWorktree` tools

When built-in worktree tools resolve in this session, a request to "make a worktree" can reach

either a tool or this skill.

.claude/worktrees/ and switches the session into it, or, given a path, switches into an

existing worktree. The model invokes it; the person does not.

directory. action: "keep" leaves the worktree and its branch on disk; action: "remove"

deletes both, but the tool will not remove a worktree that was entered by path. Since

2.1.327, remove asks first when the worktree may hold unsaved work, and is refused in

don't-ask mode or with nobody to answer unless an allow rule, auto mode or bypass permissions

mode allows it (changelog item 2.1.327-131). Since 2.1.330, neither remove nor the worktree exit dialog deletes a worktree that another running

Claude Code session is working in (changelog item 2.1.330-012).

shared helper, then enters it with EnterWorktree(path:); checks the session claim before

entering an existing worktree; owns status, cleanup, and audit.

Routing. When ExitWorktree resolves in this session, use it with action: "keep" to leave a

worktree. Use this skill to create a worktree, and never EnterWorktree(name:), which places it

inside the repository (see the nesting invariant above). Entering a worktree that already exists

also goes through this skill: run the claim gate above (check-enter), then, when

EnterWorktree resolves in this session, call EnterWorktree(path:) as the final action.

Removing a worktree is cleanup: this skill enters by path, and ExitWorktree will not remove

a worktree entered that way.

Mutation gate. EnterWorktree and ExitWorktree change the session's working directory, so

each is the final action of its step. ExitWorktree with action: "remove" deletes a worktree

and its branch, so this skill never passes it. Only cleanup deletes a worktree, under its own

confirmation.

Availability is never assumed. This section states what to do when a tool resolves in this

session, never that it is present. The four-part records live in

reference/native-worktree.md.

What this skill does not do

Integration Points

This skill complements other workflow components. It does not duplicate their logic.

| Component | Relationship |

|-----------|-------------|

| /source-control:pull-request merge (Phase 4) | Handles post-merge cleanup as part of PR lifecycle. /source-control:worktree cleanup is the standalone version for ad-hoc or batch cleanup |

| /source-control:pull-request create (Phase 2.1) | Detects default-branch checkout and suggests /source-control:worktree create |

| Project session-start hooks (if any) | May warn on main or auto-configure fresh worktrees; this skill verifies setup ran per context/create.md's post-create checks |

| Recurring maintenance tracker items | Can invoke /source-control:worktree audit periodically |

Related

Graceful Degradation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.