Worktree isolation

From schuettc/claude-code-plugins · View interactive version

Worktree isolation is an AI agent skill from the GitHub repository schuettc/claude-code-plugins. Use when about to make code changes while other work is in flight, when working in a repo's primary clone with live git worktrees, when starting a parallel line of work, or when dispatching agents that write to the tree. The rule — the primary clone is a coordination point, not a workspace; isolate

How to use this skill

Skill file

Worktree isolation

The rule

The primary clone is a coordination point, not a workspace. Every distinct line of work — a feature, a fix, a "quick change," an agent's task — runs in its own git worktree on its own branch. Two lines of work never share one checked-out tree.

This is non-negotiable when any parallel work is live: an autopilot running, another Claude session open, a long task mid-flight. The moment two actors touch one tree on different branches, you get clobbered uncommitted changes, "branch already checked out" errors, and "wait, what branch am I even on" — the classic shared-tree collision.

Why

The failure mode: Feature B has an uncommitted implementation in the working tree, and then Feature C's branch gets checked out in the same tree on top of it. You get overlaid changes, "branch already checked out" errors when something else tries to use B's branch, and confusion about which branch is even live. The autopilot already isolates its own subagents (see feature-workflow), so the exposure is everything else that can reach the same checkout — a change made directly in the primary clone, or a second Claude session opened against the repo, while parallel work is live. Nothing structural stops any of those from landing on top of in-flight work.

The fix is structural, not willpower: make isolation the easy path, so "just make the change" lands in a worktree instead of the shared tree.

How to apply (this is guidance for *you*, the agent)

When the user asks for a change and there is — or might be — parallel work live, don't edit the primary clone. Isolate first:

  1. Check where you are. If the repo has linked worktrees (git worktree list shows more than one) and you're in the primary clone, you're on the shared tree. (In a tmux setup the status bar flags this as ⚠ primary.)
  2. Isolate the work by the cheapest sufficient means:

```bash

ROOT="$(cd "$TARGET" && git rev-parse --show-toplevel)"; ROOT="$(cd "$ROOT" && pwd -P)"

git -C "$ROOT" worktree add "$ROOT/.worktrees/<branch>" -b <branch> origin/dev

```

$TARGET is the repo this line of work targets (base off dev if it exists). Then operate inside it. Best when you'll iterate interactively.

  1. Only edit the primary clone directly when nothing else is live and the change is on the clone's own checked-out branch (e.g. a dev doc tweak you're about to commit immediately).

Where worktrees go (the location rule)

Always <target-repo-root>/.worktrees/<branch> — resolved from the repo the work targets, not your current directory, and as a real path (pwd -P). Two reasons this exact form matters:

The exception: a bare primary clone

**When the primary clone is bare, <repo-root>/.worktrees/ is inside the git

directory itself — and that breaks build tooling.** Use a sibling instead:

git -C "$BARE" worktree add "${BARE}-wt/<branch>" -b <branch> origin/dev

A bare repo has no working tree, so git status there is fatal (exit 128). Any

tool that walks up from your package directory to find the repository root can

land on the bare repo rather than on your worktree, run git there, and fail —

reporting something that names neither worktrees nor bareness:

error obtaining VCS status: exit status 128
        Use -buildvcs=false to disable VCS stamping.

That is Go's -buildvcs stamping, and it fails every go build in every

worktree nested under a bare primary. It cost an hour to trace, because the error

points at VCS stamping and the actual cause is a directory layout chosen weeks

earlier. Anything else that resolves a repo root by walking upward — coverage

tools, release stampers, monorepo task runners — can hit the same wall.

-buildvcs=false silences it, at the cost of unstamped binaries, and only for

Go. Moving the worktrees out is the fix that works for every tool.

Detect it before you place a worktree: git rev-parse --is-bare-repository

returns true. If it does, use ${repo}-wt/<branch> and note it in the project's

own docs — a nested worktree that already exists will keep failing builds until

it is moved, and the error message will never say why.

Starting parallel work

To open a new parallel line of work, start it isolated from the beginning:

Knowing where you are

A worktree's git-dir path contains /worktrees/; the primary clone's does not — that's the reliable "am I isolated?" check. In a tmux status bar, surface the branch per pane and a ⚠ primary marker when a pane sits in the primary clone with worktrees live, so the shared tree is visually obvious.

Escalation — when isolation must be a hard guarantee

Worktrees + this discipline are a soft boundary: an agent can still drift back into the primary clone via absolute paths, and enforcement hooks have documented bypass paths. If you're running unattended fleets or --dangerously-skip-permissions and escape must be impossible, the real boundary is a per-agent devcontainer (filesystem-isolated, default-deny egress), not a hook. Reach for that only when the stakes require it — for interactive day-to-day parallel work, worktree-per-line-of-work is the right level.

Related

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.