Skip to content

Git worktree isolation that gives each spawned subagent its own checkout to prevent parallel-work interference.

CCM score
46 of 100
GitHub stars
201
View on GitHub

Own this plugin? Claim it

What it does

  • Adds a worktree-management skill with list, prune, and merge actions, plus a PreToolUse hook that injects isolation instructions into Agent and Task calls
  • Covers per-agent checkouts at .worktrees/<agent-name> so file edits and branch switches cannot collide across parallel agents
  • Enforces that each subagent commits inside its own worktree, with cleanup left to the orchestrator or user
  • Designed for Claude Code sessions that run multiple subagents against one repository at the same time

Agent isolation via git worktrees - gives each subagent its own checkout to prevent interference during parallel work.

Add marketplace

/plugin marketplace add TheBushidoCollective/han

Run it inside Claude Code once, then install the plugin below.

Install

/plugin install git-worktrees@han

Run it inside Claude Code after adding the marketplace.

Files

Discovered from source

Worktree Management

Name

git-worktrees:worktree-management - Manage git worktrees created by subagents

Synopsis

/worktree-management [list|prune|merge]

Description

Manages git worktrees that were created by subagents for isolation. Provides commands to list active worktrees, prune stale ones, and merge completed work back into the current branch.

Implementation

List Worktrees

Show all active worktrees and their status:

git worktree list

For worktrees in the .worktrees/ directory, also show their commit status:

for wt in .worktrees/*/; do
  if [ -d "$wt" ]; then
    echo "=== $(basename "$wt") ==="
    git -C "$wt" log --oneline -5
    echo ""
  fi
done

Prune Stale Worktrees

Remove worktrees that are no longer needed:

# Remove all worktrees in .worktrees/
git worktree list --porcelain | grep "^worktree " | grep ".worktrees/" | while read -r line; do
  wt_path="${line#worktree }"
  git worktree remove "$wt_path"
done

# Clean up any stale entries
git worktree prune

Merge Work Back

To merge a specific worktree's work into the current branch:

  1. Identify the worktree and its commits
  2. Cherry-pick or merge the commits into the main working tree
  3. Remove the worktree after successful merge
# From the main working tree:
WORKTREE_NAME="agent-xxx"
WORKTREE_BRANCH=$(git -C ".worktrees/${WORKTREE_NAME}" rev-parse HEAD)

# Cherry-pick the commits
git cherry-pick "${WORKTREE_BRANCH}"

# Or create a merge
git merge "${WORKTREE_BRANCH}" --no-ff -m "merge: integrate work from ${WORKTREE_NAME}"

# Clean up
git worktree remove ".worktrees/${WORKTREE_NAME}"

Example Interaction

User: List my active worktrees

Agent:

$ git worktree list
/path/to/repo                  abc1234 [main]
/path/to/repo/.worktrees/agent-1707123456-12345  def5678 (detached HEAD)
/path/to/repo/.worktrees/agent-1707123789-67890  ghi9012 (detached HEAD)

=== agent-1707123456-12345 ===
def5678 feat: implement user authentication
abc1234 Initial commit

=== agent-1707123789-67890 ===
ghi9012 fix: resolve database connection issue
abc1234 Initial commit

User: Merge the auth worktree and clean up

Agent: Cherry-picks the auth commit, removes the worktree, and prunes stale entries.

Readme

Git Worktrees

Agent isolation via git worktrees. When Claude Code spawns subagents (via the Agent tool), they all operate in the same working directory by default. This means agents can interfere with each other - one agent's file edits or branch switches can corrupt another agent's work. This plugin solves that by instructing each subagent to create and work in its own isolated git worktree.

OMP sessions use their harness-assigned workspace and native task isolation and handoff instead of the automatic worktree procedure below. Explicit worktree management requested by the user remains available.

How It Works

This plugin uses a PreToolUse hook on the Agent tool (and legacy Task tool) to inject worktree isolation instructions into every subagent's prompt. When a subagent is spawned:

  1. The hook runs worktree-context.sh which generates markdown instructions
  2. These instructions are wrapped in <subagent-context> tags via han hook wrap-subagent-context
  3. In OMP, the subagent uses its harness-assigned workspace. In other harnesses, it receives instructions to create an isolated worktree before modifying files.

In other harnesses, each subagent creates its own worktree at .worktrees/<agent-name> and performs all file operations there. This prevents:

  • File edit conflicts between parallel agents
  • Branch switch interference
  • Uncommitted change corruption

Installation

han plugin install git-worktrees

What Gets Injected

For harnesses other than OMP, each subagent prompt is prepended with instructions to:

  1. Create a worktree: git worktree add .worktrees/<name> --detach HEAD
  2. Use the worktree path for all file operations
  3. Commit changes within the worktree before completing
  4. Leave cleanup to the orchestrator or user

Managing Worktrees

Use the /worktree-management skill to list, prune, or merge worktrees:

/worktree-management list    # Show active worktrees
/worktree-management prune   # Remove stale worktrees
/worktree-management merge   # Merge a worktree's work back

Or manage manually:

# List all worktrees
git worktree list

# Remove a specific worktree
git worktree remove .worktrees/agent-xxx

# Prune stale entries
git worktree prune

Gitignore

Add .worktrees/ to your .gitignore to avoid tracking worktree directories:

.worktrees/

Requirements

  • Git 2.5+ (worktree support)
  • Running in a git repository

Details

Marketplace
TheBushidoCollective/han
Source
plugins/patterns/git-worktrees
Category
Pattern
Keywords
git, worktree, isolation, subagent, parallel, multi-agent
Listed since
Apr 7, 2026

More from TheBushidoCollective/han

All 340

Similar to git-worktrees

More Git & Releases