ak-worktree

Create, inspect and clean isolated git worktrees, including monorepo and submodule workflows.

SKILL.md

GitHub

Git Worktree

Create an isolated git worktree for parallel feature development.

Workflow

Step 1: Get Repo Info

Reuse current repository/worktree evidence if unchanged. Check whether an appropriate worktree already exists before creating another.

node scripts/worktree.cjs info --json

Parse JSON response for: repoType, baseBranch, projects, worktreeRoot, worktreeRootSource, dirtyState, dirtyDetails.

Step 2: Detect Branch Naming Mode

Check for exact branch name first: If caller provides a pre-formed branch name (contains uppercase letters, issue tracker keys like ABC-1234, forward slashes for multi-segment conventions like user/type/feature, or explicitly says "use this exact branch name"): → Use --no-prefix flag — skip Step 3, pass name directly as slug. Examples:

  • "ND-1377-cleanup-docs" → --no-prefix → branch ND-1377-cleanup-docs
  • "kai/feat/604-startup-option" → --no-prefix → branch kai/feat/604-startup-option

Otherwise, detect prefix from description:

  • "fix", "bug", "error", "issue" → fix
  • "refactor", "restructure", "rewrite" → refactor
  • "docs", "documentation", "readme" → docs
  • "test", "spec", "coverage" → test
  • "chore", "cleanup", "deps" → chore
  • "perf", "performance", "optimize" → perf
  • Default → feat

Step 3: Convert to Slug

Skip if --no-prefix was chosen in Step 2.

"add authentication system" → add-auth "fix login bug" → login-bug Max 50 chars, kebab-case.

Step 4: Handle Monorepo

If repoType === "monorepo" and project not specified, use ask_user capability:

ask_user capability({
  questions: [{
    header: "Project",
    question: "Which project for the worktree?",
    options: projects.map(p => ({ label: p.name, description: p.path })),
    multiSelect: false
  }]
})

Step 5: Execute

Monorepo:

node scripts/worktree.cjs create "<PROJECT>" "<SLUG>" --prefix <TYPE>

Standalone:

node scripts/worktree.cjs create "<SLUG>" --prefix <TYPE>

Options:

  • --prefix - Branch type: feat|fix|refactor|docs|test|chore|perf
  • --base <branch> - Override auto-detected base branch (default: dev→develop→main→master)
  • --checkout-submodules - Run git submodule update --init --checkout --recursive in the new worktree after create
  • --no-prefix - Skip branch prefix and preserve original case and slashes (for Jira keys, multi-segment branches like user/type/feature)
  • --worktree-root <path> - Override default location (only if needed)
  • --json - JSON output
  • --dry-run - Preview

Step 6: Install Dependencies

Install only when the requested work needs dependencies and the worktree lacks a usable environment. Use the repository's package manager and lockfile-preserving command. These are manager examples, not a mandatory setup checklist:

  • bun.lock → bun install
  • pnpm-lock.yaml → pnpm install
  • yarn.lock → yarn install
  • package-lock.json → npm install
  • poetry.lock → poetry install
  • requirements.txt → pip install -r requirements.txt
  • Cargo.toml → cargo build
  • go.mod → go mod download

Track commands/PIDs/ports for any background processes started in the worktree and stop only owned processes before removal. Inspect dirty files and nested repositories before cleanup; preserve uncommitted/user data and obtain specific authorization before destructive removal.

Commands

Command Usage Description
create create [project] <feature> Create worktree
remove remove <name-or-path> Remove worktree
info info Repo info with worktree location
list list List worktrees
status status Inspect worktree health, normalized paths, and base-branch divergence
prune prune Clean stale worktree metadata (--dry-run supported)

JSON Output Fields

When using --json, the command surfaces these high-signal fields:

Field Description
baseBranch Branch the worktree is based on
baseBranchSource "explicit" (from --base) or "auto-detected"
checkoutSubmodules Whether create will initialize submodules after checkout
currentWorktree Current worktree health record from status --json
worktrees Normalized worktree records from list --json or status --json
entries Prune output lines from prune --json
worktreePath Absolute path to the created worktree
worktreeRootSource How location was determined: --worktree-root flag, agentkit project config, agentkit user config, WORKTREE_ROOT env, superproject (<name>), monorepo internal, or sibling directory
warnings Non-fatal notices, e.g. an absolute worktree.root in project config being skipped

Notes

  • Script auto-detects superproject, monorepo, and standalone repos
  • Default worktree location, highest priority first: --worktree-root flag > worktree.root in project .agentkit/config.yaml > worktree.root in user ~/.agentkit/config.yaml > WORKTREE_ROOT env var > superproject > monorepo > sibling
  • Use --worktree-root only to override defaults
  • Use --base for long-lived variant branches (e.g., main-dsl) that diverge from auto-detected base
  • status normalizes the main checkout path in submodule repos before reporting worktree health
  • prune --dry-run is the safe first pass when auditing stale metadata
  • Env templates (.env*.example) auto-copied with .example suffix removed

Configuring a default worktree root

Persist a default so create never has to pass --worktree-root by hand:

ak config prefs set worktree.root ../my-app-worktrees --scope project
ak config prefs set worktree.root /Volumes/dev/worktrees --scope user
ak config prefs unset worktree.root --scope project
  • Project scope (.agentkit/config.yaml, usually committed) only honors a relative path, resolved against the project root — never process.cwd(). An absolute value there is skipped with a warning and resolution falls through to the next scope, because a committed project config can arrive from an untrusted clone.
  • User scope (~/.agentkit/config.yaml) accepts either a relative path (also resolved against the project root) or an absolute path, which is the right choice for a portable-drive or external-volume worktree layout.
  • The WORKTREE_ROOT environment variable still works, but now ranks below both config scopes — set it for a one-off session override, use worktree.root for anything you want to persist.
  • --worktree-root on the CLI always wins over every other source.

Editor/host integration: Claude Code's own WorktreeCreate and WorktreeRemove hooks (https://code.claude.com/docs/en/worktrees#non-git-version-control) and Codex's Worktree root setting (https://learn.chatgpt.com/docs/environments/git-worktrees) manage worktrees independently of this skill. worktree.root only changes where ak-worktree's own create command places a new worktree; it has no effect on worktrees the host application creates through its native mechanism.

Workflow Position

Typically precedes: /ak-cook (implement in worktree), /ak-fix (fix in worktree) Setup skill — creates isolated environment before implementation.