Skip to content
Development
Skill

/worktree-management

Scaffolding worktree lifecycle, diagnostics, and recovery. TRIGGER when: debugging worktree isolation, recovering a stuck worktree, or managing task isolation. SKIP: ordinary branch/commit/merge operations (use git-operations).

From plugin
scaffolding
1639 skills13 agents21 commands25 hooks

Claude Code

$ npx -y skills add komluk/scaffolding --skill worktree-management --agent claude-code

SKILL.md

worktree-management.SKILL.md
name: worktree-management
description: "Scaffolding worktree lifecycle, diagnostics, and recovery. TRIGGER when: debugging worktree isolation, recovering a stuck worktree, or managing task isolation. SKIP: ordinary branch/commit/merge operations (use git-operations)."

Worktree Management Skill

Purpose

Scaffolding.tool-specific worktree lifecycle management, diagnostics, and recovery procedures.

Live worktrees: !`git worktree list 2>/dev/null || true`

CRITICAL: Only gitops Touches Git

Other agents (developer, architect) do NOT commit. The worktree flow is: 1. Agent writes code/tests in worktree 2. **gitops** commits, merges to main, pushes, **cleans up (MANDATORY)**

If a worktree has no new commits after an agent finishes, the changes are uncommitted — gitops must commit them BEFORE merge.

Parallel Teammates: Isolation + Serialized Merge

When Agent Teams is enabled (`CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1`, see `docs/agent-teams.md`), multiple writer teammates may run concurrently. The worktree model makes this safe ONLY if both hold:

  • **Per-teammate isolation:** every writer gets its OWN worktree

`.scaffolding/worktrees/{task_id[:12]}` AND its OWN unique `SCAFFOLDING_TASK_ID`. No two writers share a working tree or a `/tmp/scaffolding-mtime-${TASK_ID}.json` staleness store — sharing would trip `file-staleness-check.sh` between them and produce unattributable interleaved changes.

  • **Serialized merge:** gitops remains the SOLE committer and merges worktree

branches to main ONE AT A TIME through the `merging`→`merged` states below. Parallel work is allowed; parallel committing/merging is not — only one merge is ever in flight.

Worktree Lifecycle

create_worktree() --> agent executes (no commit) --> gitops: commit
    --> gitops: merge_to_main() --> gitops: push --> gitops: cleanup

States (WorktreeStatus enum)

| Status | Meaning | |--------|---------| | `ready` | Created, waiting for task execution | | `merging` | Merge in progress | | `merged` | Successfully merged to main | | `conflict` | Merge conflict detected | | `cleanup` | Being removed | | `removed` | Fully cleaned up |

File Locations

| Resource | Path | |----------|------| | Worktrees directory | `.scaffolding/worktrees/` (local real dir, **never on NFS**) | | Task worktree | `.scaffolding/worktrees/{task_id[:12]}/` — branch `scaffolding/{task_id[:12]}` | | Worktree metadata | `.scaffolding/worktrees/{task_id[:12]}/worktree.json` | | Thread worktree | `.scaffolding/worktrees/threads/{id8}/` | | Named worktree | `.scaffolding/worktrees/<name>/` — plugin `WorktreeCreate` hook, branch `worktree-<name>` (`/` in the name becomes `+`) |

`.claude/worktrees/` is no longer used.

Conversations inside a worktree

Inside a worktree, `.scaffolding/conversations` is a symlink to the same share target as the main checkout (`readlink -f` of the main link). A spec written from a worktree therefore lands on the share and survives worktree removal. The `WorktreeCreate` hook creates this link; the `WorktreeRemove` hook deletes only the link, never the target. Do not copy conversation files into a worktree, and do not `rm -rf` through the link.

The `WorktreeRemove` hook only removes registered worktrees under the main checkout's `.scaffolding/worktrees/`. Dirs without `worktree.json` are not platform-made; the platform sweep must leave them alone.

Diagnostics

List All Worktrees

git worktree list
ls -la .scaffolding/worktrees/

Find Uncommitted Changes

for d in .scaffolding/worktrees/*/; do
  changes=$(cd "$d" && git status --porcelain 2>/dev/null | wc -l)
  if [ "$changes" -gt 0 ]; then
    echo "UNCOMMITTED: $d ($changes files)"
  fi
done

Recovery Procedures

Force Remove Corrupt Worktree

git worktree remove --force .scaffolding/worktrees/{task_id[:12]}
git branch -D scaffolding/{task_id[:12]}
rm -rf .scaffolding/worktrees/{task_id[:12]}
git worktree prune

Recover Uncommitted Work

cd .scaffolding/worktrees/{task_id[:12]}
git add -A && git stash
cd /project/root && git stash pop

Post-Finish Cleanup (MANDATORY after every merge or discard)

Cleanup is the final step of every merge, for ANY worktree gitops worked in (`.scaffolding/worktrees/...`, `~/.cache/worktrees/...`, anywhere). Do it for squash-merged PRs too.

# from the main checkout, never from inside the worktree
git -C <worktree> status --porcelain --untracked-files=no   # must be empty
git worktree remove <worktree>     # --force only for untracked build output (node_modules)
git branch -D <branch>
git worktree prune
  • Uncommitted tracked changes -> REFUSE, keep worktree + branch, report the files.
  • Report the cleanup in the final report.
  • Never delete remote branches here.

Note: Only **merge** and **discard** clean up. PR-open and keep actions leave the worktree intact (clean up after the PR is merged).

Sweeping stale worktrees: `/worktree-gc`

`WorktreeRemove` only sees worktrees made by `worktree-create.sh`. Plain `git worktree add` ones (e.g. `~/.cache/worktrees/<name>`) and squash-merged branches are caught by `scripts/worktree-gc.sh`:

bash "${CLAUDE_PLUGIN_ROOT}/scripts/worktree-gc.sh"                      # dry run
bash "${CLAUDE_PLUGIN_ROOT}/scripts/worktree-gc.sh" --apply              # remove
bash "${CLAUDE_PLUGIN_ROOT}/scripts/worktree-gc.sh" --all-repos ~/repos  # every repo under a dir

REMOVE only if no uncommitted tracked changes AND the work is in `origin/<default>` (no commits ahead, patch-equivalent, or squash-merged: remote ref gone + commit subjects in `origin/<default>`). Unsure -> KEEP.

Read more
@komluk
Ships withscaffolding

Spec-driven multi-agent orchestration for Claude Code — pure markdown, zero backend, runs on the stock runtime. 13 agents, 39 skills, 21 commands, 20 hooks, per-phase model tiers, opt-in lifecycle hooks, optional cross-device semantic memory.

1639 skills13 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
16
Star history of komluk/scaffolding on GitHub, a chart by star-history.com

Other skills on scaffolding.

agent-comms

agent-comms

SendMessage recipient validation and worktreePath safety (CWE-59). TRIGGER when: validating a SendMessage `to:` recipient against…

@komluk@komlukInstall Skill
agent-memory

agent-memory

Repo-local file memory under .scaffolding/ (shared/agent/conversation tiers). TRIGGER when: reading or writing .scaffolding/…

@komluk@komlukInstall Skill
api-design

api-design

RESTful API design standards: resource naming, HTTP methods, status codes, pagination, versioning. TRIGGER when: designing new…

@komluk@komlukInstall Skill
context-engineering

context-engineering

Optimize Claude Code context-window usage for accuracy and cost. TRIGGER when: hitting context limits, structuring prompts for an…

@komluk@komlukInstall Skill
database-optimization

database-optimization

Schema design, index strategy, migration safety, and query analysis. TRIGGER when: designing tables or indexes, writing a…

@komluk@komlukInstall Skill
distill

distill

Knowledge distillation: candidate extraction, confidence scoring, tier routing. Invoked by /learn and the memory-ingest Stop…

@komluk@komlukInstall Skill

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills11d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc13d293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react2d252k
@facebook@facebookInstall Skill

Questions about worktree-management.

Which coding agents can use worktree-management?
worktree-management installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.