Using git worktrees

Moves feature work into an isolated git worktree: checks whether the agent is already isolated, prefers the harness's own worktree tool, keeps the folder git-ignored, installs dependencies and runs a baseline test. Use when the work must not land on the branch you have open.

Git & PRsPack: Superpowers167 linesusing-git-worktrees
Author
Source
obra/superpowers/skills/using-git-worktrees @ 22f0b46
License
MIT · Copyright (c) 2025 Jesse Vincent

About this skill

What it does

The skill puts feature work in an isolated workspace, so it does not land on the branch you have open or mix with its uncommitted files. First it checks whether the agent is already in a linked worktree by comparing git rev-parse --git-dir with --git-common-dir, guarding against submodules, which pass the same test. If so, nothing is created; if not, the agent asks first unless your instructions already answer that.

It prefers the harness's own tool — an EnterWorktree-style tool, a /worktree command or a --worktree flag — because a worktree made behind the harness's back is state it cannot see or clean up. Only without one does it run git worktree add: in the folder your instructions name, else an existing .worktrees/ or worktrees/, else .worktrees/; a folder inside the project must pass git check-ignore, and if it does not, the agent adds it to .gitignore and commits that on your current branch before going on. Then it installs dependencies by what it finds (package.json, Cargo.toml, go.mod and others) and runs the tests for a clean baseline. If the sandbox blocks creation, it says so and works in place.

The folder holds only SKILL.md and the licence.

When to use it, and when not

Use it before a feature or a plan, when the work should not land on the branch you have open — especially with uncommitted changes there or several agent sessions in one repository.

It is not needed for a small fix you are happy to commit on the current branch. If you decline the worktree, the skill only installs dependencies and runs the tests in place; a red baseline makes it stop and ask.

How to run it

The install command below puts the folder in place. The agent picks the skill up by its description; to call it yourself:

  • Claude Code: /using-git-worktrees, or ask for the skill by name.
  • Codex: $using-git-worktrees.
  • Cursor: type / in the Agent chat and pick using-git-worktrees.

Example: /using-git-worktrees Set up an isolated workspace for the invoice-export feature on a new branch and tell me whether the baseline tests pass.

executing-plans and subagent-driven-development call this skill during setup, and finishing-a-development-branch removes the worktrees it made once the work is merged. Install that one too.

How it differs from similar skills

Small steps builder governs how you work — one change, run it, commit — and leaves open where the checkout lives; this skill settles that before the first step. Safe git recovery repairs what is already broken or lost; a separate worktree keeps experiments off your branch, so there is less to repair. Git workflow and versioning gives worktrees a short section for agents working in parallel: one directory per branch, removed after the merge. It does not look for an existing worktree or the harness's own tool and runs no baseline tests.

Install

Run one command — it saves SKILL.md and the files it uses into your agent's skills folder. Claude Code and Codex pick up a new skill without a restart; if it does not show up, restart the agent.

Claude Code — macOS, Linux
curl -fsSL --create-dirs -o ~/.claude/skills/using-git-worktrees/SKILL.md https://anymodel.org/en/skills/superpowers/using-git-worktrees/SKILL.md -o ~/.claude/skills/using-git-worktrees/LICENSE https://anymodel.org/en/skills/superpowers/using-git-worktrees/LICENSE
Codex, Cursor — macOS, Linux
curl -fsSL --create-dirs -o ~/.agents/skills/using-git-worktrees/SKILL.md https://anymodel.org/en/skills/superpowers/using-git-worktrees/SKILL.md -o ~/.agents/skills/using-git-worktrees/LICENSE https://anymodel.org/en/skills/superpowers/using-git-worktrees/LICENSE
Claude Code — Windows PowerShell
foreach ($f in 'SKILL.md','LICENSE') { $p = Join-Path "$HOME\.claude\skills\using-git-worktrees" $f; New-Item -ItemType Directory -Force (Split-Path $p) | Out-Null; Invoke-WebRequest "https://anymodel.org/en/skills/superpowers/using-git-worktrees/$f" -UseBasicParsing -OutFile $p }
Codex, Cursor — Windows PowerShell
foreach ($f in 'SKILL.md','LICENSE') { $p = Join-Path "$HOME\.agents\skills\using-git-worktrees" $f; New-Item -ItemType Directory -Force (Split-Path $p) | Out-Null; Invoke-WebRequest "https://anymodel.org/en/skills/superpowers/using-git-worktrees/$f" -UseBasicParsing -OutFile $p }

Any other agent: save the file into its skills folder, or give it the link to the file: https://anymodel.org/en/skills/superpowers/using-git-worktrees/SKILL.md

SKILL.md

Raw

Using Git Worktrees

Overview

Ensure work happens in an isolated workspace. Prefer your platform's native worktree tools. Fall back to manual git worktrees only when no native tool is available.

Core principle: Detect existing isolation first. Then use native tools. Then fall back to git. Never fight the harness.

Announce at start: "I'm using the using-git-worktrees skill to set up an isolated workspace."

Step 0: Detect Existing Isolation

Before creating anything, check if you are already in an isolated workspace.

GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
BRANCH=$(git branch --show-current)

Submodule guard: GIT_DIR != GIT_COMMON is also true inside git submodules. Before concluding "already in a worktree," verify you are not in a submodule:

# If this returns a path, you're in a submodule, not a worktree — treat as normal repo
git rev-parse --show-superproject-working-tree 2>/dev/null

If GIT_DIR != GIT_COMMON (and not a submodule): You are already in a linked worktree. Skip to Step 2 (Project Setup). Do NOT create another worktree.

Report with branch state:

  • On a branch: "Already in isolated workspace at <path> on branch <name>."
  • Detached HEAD: "Already in isolated workspace at <path> (detached HEAD, externally managed). Branch creation needed at finish time."

If GIT_DIR == GIT_COMMON (or in a submodule): You are in a normal repo checkout.

Has the user already indicated their worktree preference in your instructions? If not, ask for consent before creating a worktree:

"Would you like me to set up an isolated worktree? It protects your current branch from changes."

Honor any existing declared preference without asking. If the user declines consent, work in place and skip to Step 2.

Step 1: Create Isolated Workspace

You have two mechanisms. Try them in this order.

1a. Native Worktree Tools (preferred)

The user has asked for an isolated workspace (Step 0 consent). Do you already have a way to create a worktree? It might be a tool with a name like EnterWorktree, WorktreeCreate, a /worktree command, or a --worktree flag. If you do, use it and skip to Step 2.

Native tools handle directory placement, branch creation, and cleanup automatically. Using git worktree add when you have a native tool creates phantom state your harness can't see or manage.

Only proceed to Step 1b if you have no native worktree tool available.

1b. Git Worktree Fallback

Only use this if Step 1a does not apply — you have no native worktree tool available. Create a worktree manually using git.

Directory Selection

Follow this priority order. Explicit user preference always beats observed filesystem state.

  1. Check your instructions for a declared worktree directory preference. If the user has already specified one, use it without asking.

  2. Check for an existing project-local worktree directory:

    ls -d .worktrees 2>/dev/null     # Preferred (hidden)
    ls -d worktrees 2>/dev/null      # Alternative
    

    If found, use it. If both exist, .worktrees wins.

  3. If there is no other guidance available, default to .worktrees/ at the project root.

Safety Verification (project-local directories only)

MUST verify directory is ignored before creating worktree:

git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null

If NOT ignored: Add to .gitignore, commit the change, then proceed.

Why critical: Prevents accidentally committing worktree contents to repository.

Create the Worktree
# Determine path based on chosen location
path="$LOCATION/$BRANCH_NAME"

git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"

Sandbox fallback: If git worktree add fails with a permission error (sandbox denial), tell the user the sandbox blocked worktree creation and you're working in the current directory instead. Then run setup and baseline tests in place.

Step 2: Project Setup

Auto-detect and run appropriate setup:

# Node.js
if [ -f package.json ]; then npm install; fi

# Rust
if [ -f Cargo.toml ]; then cargo build; fi

# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi

# Go
if [ -f go.mod ]; then go mod download; fi

Step 3: Verify Clean Baseline

Run tests to ensure workspace starts clean:

# Use project-appropriate command
npm test / cargo test / pytest / go test ./...

If tests fail: Report failures, ask whether to proceed or investigate.

If tests pass: Report ready.

Report

Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>

Quick Reference

Situation Action
Already in linked worktree Skip creation (Step 0)
In a submodule Treat as normal repo (Step 0 guard)
Native worktree tool available Use it (Step 1a)
No native tool Git worktree fallback (Step 1b)
.worktrees/ exists Use it (verify ignored)
worktrees/ exists Use it (verify ignored)
Both exist Use .worktrees/
Neither exists Check instruction file, then default .worktrees/
Directory not ignored Add to .gitignore + commit
Permission error on create Sandbox fallback, work in place
Tests fail during baseline Report failures + ask
No package.json/Cargo.toml Skip dependency install

Common Rationalizations

Excuse Reality
"I'm obviously not in a worktree — no need to check" Run Step 0. Harness-created isolation and submodules both fool eyeballing; the detection commands settle it.
"git worktree add is quicker than hunting for a native tool" A native tool (e.g. EnterWorktree) owns placement, branching, and cleanup. Bypassing it is the #1 mistake — it creates phantom state your harness can't see or manage.
"The worktree directory is surely ignored already" Run git check-ignore. An unignored worktree directory commits the whole tree into the repo.
"Any directory name works" Explicit instructions beat an existing project-local directory, which beats the .worktrees/ default.
"The workspace is fresh — baseline tests can wait" A dirty baseline makes every later failure ambiguous. Run the tests now; proceeding past failures is your human partner's call.

Connect through AnyModel API

This skill runs on whatever model your agent uses. Point Claude Code, Codex or Cursor at AnyModel to run it on Claude, GPT, Gemini and more with one key — pay per token, no subscription.

More from the pack Superpowers

Brainstorming before you build

Finds out what you want to build and why: one question at a time, its understanding played back part by part, two or three approaches to pick from and a design doc, with no code until you approve it. Use for a new feature or change when the request leaves the essentials open.

Dispatching parallel agents

Splits independent problems between parallel subagents, each with one domain and a self-contained brief with scope, constraints and the report expected back, then checks their edits for conflicts and runs the full suite. Use when several unrelated things fail at once.

Executing plans

Runs a plan from writing-plans in the current session with no subagent per task: tasks in order through TDD, a ledger of progress and rulings that survives context compaction, and one fresh review of the whole branch at the end. Use when per-task review is not needed or there are no subagents.

Finishing a development branch

Finishes a branch: runs the tests, offers to merge locally, open a pull request or keep it as is, reruns the tests after a merge and removes its own worktree, and discards work only after you type "discard". Use when the implementation is done.

Receiving code review

Checks each review comment against the code before changing anything, clears up every unclear item first, pushes back with technical reasons and never performs agreement. Use when working through review feedback without blind fixes.

Requesting code review

Hands finished work to a fresh reviewer subagent through a template: the commit range, the plan or requirements, findings by severity and a verdict on merging. Use when a task or feature is done and a merge to main is next.

Subagent-driven development

Executes a plan through subagents: a fresh implementer and reviewer for every task, checking spec compliance and code quality, up to five fix rounds and a final review of the whole branch. Use for a plan of independent tasks when each one needs a review.

Systematic debugging

Keeps the agent from guessing at fixes: reproduce the bug and find the root cause first, test one hypothesis at a time, fix with a failing test, and question the design after three failed fixes. Use for any bug, test failure or unexpected behavior.

Test-driven development, Superpowers style

Drives work test-first: a test seen failing, then the least code that passes and a run of the whole suite; code written before its test is deleted. Use for features and bug fixes when the agent tends to add tests after the code.

Verification before completion

Stops the agent from saying "done" or "tests pass" until it has rerun the check and read the whole output. Use whenever the agent reports success, commits or opens a pull request.

Writing implementation plans

Turns a spec into a plan for an implementer new to the codebase: files, signatures, interfaces between tasks, tests with exact values and TDD steps with each command's expected output. Use for a multi-step task when the spec is ready and no code exists yet.

Writing skills

Writes and edits skills the TDD way: a subagent first does the task without the skill and its mistakes are written down, then the smallest text that answers them goes in and the loopholes get closed. Use for a skill that other agents will load.