Imported from Moonrainbowh/superpowers-easy (
skills/using-git-worktrees/SKILL.md). Install upstream withnpx skills add Moonrainbowh/superpowers-easy --skill using-git-worktrees. Copyright stays with the author.
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."
Standalone Invocation
Direct $using-git-worktrees invocation does not require a prior spec or full Superpowers plan.
First inspect the repository read-only. If no parent execution contract exists, ask one bundled
question for the exact in-place/worktree location or native owner, branch/base and current starting
HEAD, pre-existing dirty paths to preserve, dependency/network authorization, baseline-failure
policy, commit permission for any repository setup edit, and desired ownership/cleanup. The answer
is the standalone workspace contract. Skip planning-document hash handoff when no approved planning
documents exist. If the user's direct prompt already supplies every relevant field, proceed without
another confirmation; informational/advice requests remain read-only.
Approval Timing
Workspace and branch handling belong in the combined execution contract. During discovery, surface an unresolved preference with the other material questions. After the contract is approved, honor its exact choice without asking again. The contract must identify in-place work or the exact worktree location/native owner and source-document handoff. If an approved contract omits this material workspace choice, batch it as one blocker; do not create a worktree or silently default to a new filesystem location after approval.
The same contract must state whether dependency installation/network access is authorized, how pre-existing baseline failures are handled, and whether planning documents and implementation changes may be committed.
The contract's starting HEAD is also an immutable code-baseline anchor. Before creating a worktree,
require git rev-parse HEAD in the approved source checkout to equal that exact SHA. Create the
target branch from the approved SHA, not whichever HEAD happens to be current at execution time.
After native or manual worktree creation, require the target HEAD to equal the same SHA before
transferring documents or editing files. A mismatch is a single material code-baseline blocker; do
not reset, rebase, or silently update the approved anchor.
Approved Contract Handoff
When an approved spec and plan exist, they may be uncommitted in the original checkout. Before creating or entering a different worktree:
- Load the repository-relative paths and literal hashes from the approved-hash manifest produced at the combined review. First require every current source document to equal its approved hash.
- Record whether each source path was created by this workflow and previously absent/untracked, or whether it contained pre-existing/tracked content whose handling was decided in the contract.
- After entering the target worktree, inspect each destination before copying. If it exists — tracked or untracked — require its hash to equal the approved hash and do not overwrite it. If it is absent, copy the document to the same repository-relative path using native file tools.
- Recompute the target hashes and require
current source == approved hash == targetbefore implementation starts. - When the contract explicitly authorizes it, remove workflow-created, previously absent/untracked source documents only after the verified target copies exist, then verify the original checkout no longer reports those paths. This is a verified move, preventing duplicate untracked files from blocking a later merge. Otherwise preserve the source copies; if that would conflict with the approved delivery, stop at the single workspace-boundary blocker. Never delete, reset, or overwrite pre-existing/tracked source content.
- If the contract authorizes commits, include the approved planning documents in the first appropriate commit on the feature branch. If it requires uncommitted delivery, leave them uncommitted and pass their absolute target-worktree paths to execution and review tools.
This transfer is mechanical execution of the approved contract, not another approval gate. A hash mismatch or any conflicting destination file is a material blocker; do not silently choose one version or replace the approved manifest with a newly observed source hash.
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. Verify
its HEAD equals the approved starting HEAD and that the contract selected this workspace, then 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.
Read the approved execution contract for the worktree preference. Honor it without asking. If the user chose in-place work, skip to Step 2. If this is still pre-approval discovery, add the preference to the combined question/review rather than creating a separate gate. If an approved contract omitted the choice, stop at the single material workspace blocker described in Approval Timing.
Step 1: Create Isolated Workspace
You have two mechanisms. Try them in this order.
1a. Native Worktree Tools (preferred)
The execution contract selected an isolated workspace and its owner/location. Do you already have a
way to create that worktree? It might be a tool with a name like EnterWorktree,
WorktreeCreate, a /worktree command, or a --worktree flag. If so, use it and skip to Step 2.
Native tools handle directory placement and branch creation, and may own later cleanup. Cleanup still
requires the approved contract and must use the native mechanism when host-owned. 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.
-
Check your instructions for a declared worktree directory preference. If the user has already specified one, use it without asking.
-
Check for an existing project-local worktree directory:
ls -d .worktrees 2>/dev/null # Preferred (hidden) ls -d worktrees 2>/dev/null # AlternativeIf found, use it. If both exist,
.worktreeswins. -
If the approved contract does not resolve the location, stop at the single material workspace blocker. Filesystem conventions may inform the pre-approval contract, but do not replace it after approval.
Safety Verification (project-local directories only)
MUST verify directory is ignored before creating worktree:
git check-ignore -q -- "$LOCATION" 2>/dev/null
If NOT ignored: Do not create the worktree there and do not modify or commit .gitignore
implicitly. Use an already approved external worktree location that does not sit inside the
repository. Change .gitignore only when the combined contract explicitly authorizes that exact
repository edit, and commit it only when the commit policy authorizes a commit at that point. If no
safe authorized location or edit exists, batch this as the single workspace-boundary blocker.
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" "$APPROVED_STARTING_HEAD"
cd "$path"
test "$(git rev-parse HEAD)" = "$APPROVED_STARTING_HEAD"
Record the exact absolute path, creation mechanism, and ownership in execution state immediately. Native/harness-created worktrees remain host-owned unless the tool explicitly returns ownership to this workflow; a manual worktree created by this step is workflow-owned at that exact recorded path.
Sandbox fallback: If git worktree add fails with a permission error, use an already approved
in-place fallback if the contract contains one. Otherwise, working in the current directory changes
the locked workspace boundary; batch that blocker into one question instead of silently falling
back.
Step 2: Project Setup
Auto-detect required setup. Run dependency installation commands only when the execution contract authorizes dependency changes and any required network access. Otherwise use the existing environment; if a missing dependency blocks execution, treat the install as an unapproved external action and ask once rather than running it implicitly.
# 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: Investigate enough to distinguish a pre-existing unrelated failure from a failure that invalidates the plan. Follow the approved baseline-failure policy. Record isolated pre-existing failures and continue with focused verification only when the policy permits it and the acceptance criteria remain trustworthy. Batch one question only if the failure changes scope, makes completion evidence unreliable, contradicts the policy, or remains an unresolved blocker.
If the baseline is fully green: Report ready with the passing command and counts.
If the approved policy allows documented exceptions: Report the exact failing command, pre-existing unrelated failure signatures, baseline evidence, and focused checks that remain valid. Never use the all-green report in this branch.
Report
Worktree ready at <full-path>
Baseline passing: <command> (<N> tests, 0 failures)
Ready to implement <feature-name>
Or, only under an approved exception policy:
Worktree ready at <full-path>
Baseline exception: <command> has <N> documented pre-existing unrelated failures
Valid focused checks: <commands and results>
Ready to implement <feature-name> under the approved baseline policy
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) |
Approved location is .worktrees/ and it exists |
Use it (verify that exact location is ignored) |
Approved location is worktrees/ and it exists |
Use it (verify that exact location is ignored) |
| Both exist | Use only the location named in the approved contract |
| Neither exists | Use the exact approved location; if absent from the contract, ask once |
| Directory not ignored | Use an approved external location; edit/commit .gitignore only when exactly authorized, otherwise ask once |
| Permission error on create | Use approved fallback; otherwise report one workspace-boundary blocker |
| Tests fail during baseline | Obey the policy: fully green blocks; documented exceptions continue only with exact evidence |
| No package.json/Cargo.toml | Skip dependency install |
Common Mistakes
Fighting the harness
- Problem: Using
git worktree addwhen the platform already provides isolation - Fix: Step 0 detects existing isolation. Step 1a defers to native tools.
Skipping detection
- Problem: Creating a nested worktree inside an existing one
- Fix: Always run Step 0 before creating anything
Skipping ignore verification
- Problem: Worktree contents get tracked, pollute git status
- Fix: Always use
git check-ignorebefore creating project-local worktree
Assuming directory location
- Problem: Creates inconsistency, violates project conventions
- Fix: Resolve the exact location in the combined contract; do not substitute a convention later
Ignoring failing baseline tests
- Problem: Can't distinguish new bugs from pre-existing issues
- Fix: Triage and record them; escalate only when they invalidate the approved acceptance evidence
Red Flags
Never:
- Create a worktree when Step 0 detects existing isolation
- Use
git worktree addwhen you have a native worktree tool (e.g.,EnterWorktree). This is the #1 mistake — if you have it, use it. - Skip Step 1a by jumping straight to Step 1b's git commands
- Create worktree without verifying it's ignored (project-local)
- Skip baseline test verification
- Hide or ignore baseline failures that affect the approved acceptance criteria
Always:
- Run Step 0 detection first
- Prefer native tools over git fallback
- Follow the exact workspace location/owner recorded in the approved contract
- Verify directory is ignored for project-local
- Auto-detect setup; install dependencies only when authorized
- Verify or explicitly characterize the test baseline