Claude-Code-Worktrees für parallele Agenten: der worktree-Skill
Dmitriy Solodukha, Caprock-Entwickler · Veröffentlicht am
Am mit Claude Code und Codex getestet
Claude-Code-Worktrees für parallele Agenten in einem Repository: worktree-Skill richtet ein, prüft Status, mergt zurück, räumt auf, ohne --force.
Claude-Code-Worktrees geben jedem Agenten einen eigenen Ordner und Branch in einem Repository; der worktree-Skill übernimmt Einrichtung und Aufräumen in Claude Code, Codex, OpenCode und Cursor auf dieselbe Weise. Eine Aufgabe, ein Branch, ein Ordner: Er erstellt den Worktree auf aktueller Basis, kopiert benötigte env-Dateien, vergibt eigene Ports oder Datenbanken, führt einen Smoke-Test aus, meldet den Status in einer Tabelle, mergt zurück und entfernt nur saubere und gemergte Worktrees.
Installiere den worktree-Skill
Ein Befehl pro Agent. Der Skill ist eine einzelne SKILL.md; bei der Installation wird nichts ausgeführt.
Alle neun Skills als Plugin. Gib dies in Claude Code ein:
Oder nur diesen Skill, im Projektordner in einem Terminal:
Füge -g hinzu, um ihn für jedes Projekt zu installieren.
Im Projektordner in einem Terminal:
Füge -g hinzu, um ihn für jedes Projekt zu installieren.
Im Projektordner in einem Terminal:
Füge -g hinzu, um ihn für jedes Projekt zu installieren. Noch nicht mit OpenCode getestet: Der Skill besteht aus reinen Anweisungen im offenen Format, aber kein Durchlauf mit OpenCode hat ihn geprüft.
Im Projektordner in einem Terminal:
Füge -g hinzu, um ihn für jedes Projekt zu installieren. Noch nicht mit Cursor getestet: Der Skill besteht aus reinen Anweisungen im offenen Format, aber kein Durchlauf mit Cursor hat ihn geprüft.
Prompts für den worktree-Skill
Der Agent wählt den Skill, wenn deine Worte zu dessen Beschreibung passen; mit dem Namen stellst du es sicher. Klicke auf einen Prompt, um ihn zu kopieren.
Wie der worktree-Skill Claude-Code-Worktrees verwaltet
- Vor dem Erstellen:
git worktree listund ein sauberer main-Checkout; mit Caprock zeigtrecent_sessions, welche Branches durch laufende Sessions belegt sind. - Erstellen:
git worktree add -b <task> ../<repo>.worktrees/<task> main, nach der Aufgabe in kebab-case benannt. - Ausführbar machen: von Git ignorierte env-Dateien kopieren (keine Symlinks), bei benötigtem Build das projekteigene Setup ausführen, Ports und Datenbanken trennen, eine kurze Prüfung.
- Status, jederzeit:
git worktree list -v,git -C <path> status --short, die Commits vor main. - Zurückmergen aus dem main-Checkout oder einen Pull Request öffnen; bei einem Konflikt anhalten.
- Entfernen erst, nachdem
git merge-base --is-ancestor„merged“ meldet, mitgit worktree removeundgit branch -d, die nicht gemergte Arbeit ablehnen.
Jeder Git-Befehl im Skill wurde mit Gits eigener Hilfe abgeglichen (git 2.54.0, 29. November 2026).
Was wir ausgeführt haben und was die Agenten taten
Jeder Skill wurde am 29. November 2026 in einem eigens dafür angelegten Wegwerf-Repository ausgeführt, als Projekt-Skill installiert. Ein kleines Repository mit calc.py und einer von Git ignorierten .env.
Claude Code (claude -p --model sonnet)
Set up two git worktrees so two agents can work in parallel: one to add a subtract function to calc.py, one to make div raise a clear error on zero. Don't do the tasks themselves, just set up the worktrees and tell me their status.
Caprocks recent_sessions aufgerufen, ../app.worktrees/feat-subtract und ../app.worktrees/fix-div-zero auf eigenen Branches von main erstellt, in beiden einen Smoke-Test ausgeführt und die Tabelle des Skills ausgegeben. Er kopierte .env bewusst nicht, weil calc.py die Datei nicht liest, und sagte das dazu.
| Worktree | Branch | State | Ahead of main | Next | | -------- | ------ | ----- | ------------- | ---- | | `../app.worktrees/feat-subtract` | `feat-subtract` | clean | 0 | work | | `../app.worktrees/fix-div-zero` | `fix-div-zero` | clean | 0 | work |
Codex (0.163.0, codex exec)
Clean up my git worktrees, I'm done with them.
Nachdem zuvor ein Worktree lokal geändert worden war: Status und --is-ancestor geprüft, den sauberen Worktree entfernt und seinen Branch mit git branch -d gelöscht. Den mit lokalen Änderungen lehnte er unter Verweis auf die Skill-Regel ab, ohne --force.
Agenten, mit denen er getestet wurde
- Claude Code: am 29. November 2026 mit
claude -p --model sonnetgetestet, der Skill als Projekt-Skill in.claude/skills/installiert. - Codex: am 29. November 2026 mit Codex 0.163.0 (
codex exec) getestet, der Skill in.agents/skills/. - OpenCode und Cursor: nicht getestet. Der Skill besteht aus reinen Anweisungen im offenen Agent-Skills-Format, wurde aber mit keinem der beiden erprobt.
Grenzen
- Abhängigkeiten werden nur installiert, wenn die Aufgabe einen Build oder Tests benötigt: Sie kommen aus dem Netzwerk, und der Skill weist vorher darauf hin.
- Von Git ignorierte Dateien, die du im Worktree bearbeitet hast, verschwinden beim Entfernen mit dem Ordner; der Skill fordert dich auf, sie vorher herauszukopieren.
- Es wird keine Codex-spezifische Worktree-Funktion beschrieben: Keine wurde verifiziert.
Mit und ohne Caprock
Wenn der Caprock-MCP-Server verbunden ist, ruft der Skill recent_sessions für das Repository auf, um laufende Agenten-Sessions und ihre Branches zu sehen, damit eine neue Aufgabe keinen belegten Branch nimmt. Ohne ihn dient die Branch-Spalte von git worktree list als Nachweis. Caprock ist die kostenlose Open-Source-App, die jede Coding-Agent-Session auf deinem Computer erfasst.
Die vollständige SKILL.md
Die Datei aus dspv/skills in Revision dfce799. Zur manuellen Installation speicherst du sie in deinem Projekt als .claude/skills/worktree/SKILL.md für Claude Code oder .agents/skills/worktree/SKILL.md für Codex.
--- name: worktree description: Set up and clean up git worktrees so several coding agents (or you and an agent) can work in parallel on one repository without stepping on each other. One task, one branch, one folder; copies env files, installs dependencies, checks status, merges back and removes the worktree safely. Use when the user asks for claude code worktrees, parallel agents or sessions on one repo, "work in a worktree", "spin up a worktree for this task", "clean up my worktrees", or "which worktrees have unfinished work". license: Apache-2.0 --- # Worktree A git worktree is a second checkout of the same repository: its own folder, its own branch, its own index, sharing one `.git` history. Two agents in two worktrees cannot overwrite each other's files. They can still collide on the same branch, the same port or the same database, so this skill fixes those too. Every command below is in git's own help pages (`git worktree --help` and the help of each other git command used; checked with git 2.54.0 on 2026-11-29). ## Safety rules (always) 1. **Never remove a worktree that has uncommitted or untracked work.** Check first (step 4). If it is dirty, stop and tell the user what is there. 2. **Never use `--force` or `-f`** on `git worktree add`, `remove` or `move`, and never `git branch -D`. Git refuses for a reason: a branch already checked out elsewhere, a dirty tree, a locked worktree. Report the refusal instead of overriding it. Only the user may decide to force, in their own words. 3. **Never delete a worktree folder with `rm -rf`.** Use `git worktree remove`; if a folder is already gone, `git worktree prune` cleans the record. 4. **One branch per worktree, one agent per worktree.** Git will not check out a branch that is already checked out in another worktree; do not work around it. 5. **Do not touch the main checkout** while agents work in worktrees, except to merge at the end. 6. **Do not remove a locked worktree.** A lock (`git worktree list` shows `locked`) means someone or some tool is still using it. Ask the user. ## 1. Before creating: look at what exists ```bash git worktree list # main checkout first, then each linked worktree git status --short # the main checkout should be clean before you branch from it ``` If a worktree for the same task already exists, reuse it instead of making a second one. **If the Caprock MCP tools are available** (server `caprock`), call `recent_sessions` with `repo` set to the repository to see which agent sessions are running here and on which branch. A session still active on a branch means that branch is taken: give the new task its own. Without those tools, the branch column of `git worktree list` is the record. (Caprock, <https://caprock.dev>, is a free, open-source app that runs locally and provides those tools.) ## 2. Create: one task, one branch, one folder Name everything after the task, short and in kebab-case: `fix-login-redirect`, `feat-csv-export`. The branch and the folder share the name. Where to put it, in order of preference: - **Beside the repository**: `../<repo>.worktrees/<task>`. Nothing nested inside the main checkout, so search tools, test runners and file watchers in the main checkout do not see a second copy of the code. - **Inside the repository**, when the agent may only write inside it: `.worktrees/<task>`, and hide the folder from git locally without editing a tracked file: `echo ".worktrees/" >> "$(git rev-parse --git-common-dir)/info/exclude"`. - **Claude Code's own location**, when Claude Code made it (see the last section): `.claude/worktrees/<task>`. Create it on a new branch from an up-to-date base (the user's default branch, usually `main`): ```bash git worktree add -b <task> ../<repo>.worktrees/<task> main ``` To continue an existing branch instead: `git worktree add ../<repo>.worktrees/<task> <branch>`. If git answers that the branch is already checked out in another worktree, stop (rule 4). ## 3. Make the worktree runnable A worktree is a fresh checkout of tracked files only. Before handing it to an agent: 1. **Env and local config files** (gitignored, so not in the new folder): copy the ones the project needs, such as `.env` or `.env.local`, from the main checkout. Copy, do not symlink, so one agent's change does not leak into the others. Never commit them. 2. **Dependencies**: run the project's own setup in the worktree, the one its README, Makefile or package manifest names (`npm ci`, `uv sync`, `bundle install` and the like). These fetch packages from the network: run them only when the task needs a working build or tests, and say so to the user. 3. **Shared resources**: give each worktree its own dev-server port and its own local database or data folder when the project allows it (usually an env variable). Two agents on one port or one database collide even in separate folders. 4. **Smoke check**: run the project's fastest check (a build, a lint, one test file) in the worktree, so a broken setup shows up now and not halfway through the task. Then start the agent with the worktree as its working directory, and give it the task, the branch name, and these rules: commit on this branch only, do not push unless told, do not touch other worktrees. ## 4. Check status (any time, and before every merge or removal) ```bash git worktree list -v # paths, branches, locked, prunable git -C <path> status --short # empty means no uncommitted work git -C <path> log --oneline main..HEAD # the task's commits not yet in main ``` A worktree is **done** when `status --short` is empty and its commits are what the task asked for. It is **dirty** when `status --short` prints anything (`??` lines are untracked files and count as work). `prunable` means the folder is gone; only `git worktree prune` is needed. ## 5. Merge back From the main checkout, with a clean status: ```bash git switch main git merge --no-ff <task> # or open a pull request from <task> ``` If the user's flow is pull requests, with their go-ahead push the branch and open one instead, and leave the worktree in place until it is merged. On a merge conflict, stop and show the conflicting files; do not resolve by taking one side wholesale. ## 6. Remove Only after step 4 shows the worktree clean and its commits are in the base branch: ```bash git merge-base --is-ancestor <task> main && echo merged # prints merged, or nothing git worktree remove ../<repo>.worktrees/<task> # refuses a dirty tree: good git branch -d <task> # refuses an unmerged branch: good git worktree prune -n -v # shows stale records; drop -n to clean ``` If `remove` refuses, the worktree has work: report it (rule 1). If `branch -d` refuses, the branch has commits that are not merged: report them with `git log --oneline main..<task>`. Copy out any gitignored file you still need (an `.env` you edited) before removing: git does not count those as work and they go with the folder. ## Output After each run, report one table, one row per worktree, then what is left for the user: | Worktree | Branch | State | Ahead of main | Next | | -------- | ------ | ----- | ------------- | ---- | `State` is `clean`, `dirty (N files)`, `locked` or `prunable`; `Ahead of main` is the count of `git log --oneline main..<branch>`; `Next` is `work`, `merge`, `remove` or `ask user`. Then list anything you refused to do (a dirty or locked worktree, an unmerged branch) and why. ## In Claude Code Claude Code has this built in (from its docs, code.claude.com/docs/en/worktrees, read on 2026-11-29): `claude --worktree <name>` (or `-w`) creates `.claude/worktrees/<name>/` on a new branch `worktree-<name>` and starts the session there; a `.worktreeinclude` file at the project root (gitignore syntax) lists gitignored files such as `.env` to copy into each new worktree; custom subagents can run in their own worktree with `isolation: worktree`. On exit, Claude Code asks before removing a worktree that has work. Non-interactive `claude -p` runs are not cleaned up: check and remove those with steps 4 and 6. Add `.claude/worktrees/` to the project's `.gitignore` (or `info/exclude`) so they do not show as untracked files.
Fragen zum worktree-Skill
- Wie funktionieren Claude-Code-Worktrees?
- `claude --worktree <name>` (oder `-w`) erstellt `.claude/worktrees/<name>/` auf einem neuen Branch `worktree-<name>` und startet dort die Session. Eine `.worktreeinclude`-Datei listet von Git ignorierte Dateien wie `.env` auf, die hineinkopiert werden sollen.
- Was ergänzt der worktree-Skill bei Claude-Code-Worktrees?
- Das Drumherum: getrennte Ports und Datenbanken, ein Smoke-Test, eine Statustabelle für alle Worktrees, ein sicherer Merge und ein Aufräumen, das Worktrees mit lokalen Änderungen, gesperrte oder nicht gemergte Worktrees ablehnt. Er entfernt auch Worktrees aus `claude -p`-Durchläufen, die Claude Code nicht entfernt.
- Kann der worktree-Skill meine Arbeit löschen?
- Er ist so geschrieben, dass er das nicht tut: kein `--force`, kein `git branch -D`, kein `rm -rf`. Ein Worktree mit nicht committeten oder unversionierten Dateien wird gemeldet, nicht entfernt.
- Funktioniert er mit Codex?
- Ja, mit normalem Git. Codex 0.163.0 nutzte ihn im Test vom 29. November 2026, um Worktrees aufzuräumen, und lehnte den mit lokalen Änderungen ab.