Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Creates isolated git worktrees for feature branches with smart directory selection, gitignore safety checks, and project setup.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: using-git-worktrees3description: Use when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback4---56# Using Git Worktrees78## Overview910Ensure 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.1112**Core principle:** Detect existing isolation first. Then use native tools. Then fall back to git. Never fight the harness.1314**Announce at start:** "I'm using the using-git-worktrees skill to set up an isolated workspace."1516## Step 0: Detect Existing Isolation1718**Before creating anything, check if you are already in an isolated workspace.**1920```bash21GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)22GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)23BRANCH=$(git branch --show-current)24```2526**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:2728```bash29# If this returns a path, you're in a submodule, not a worktree — treat as normal repo30git rev-parse --show-superproject-working-tree 2>/dev/null31```3233**If `GIT_DIR != GIT_COMMON` (and not a submodule):** You are already in a linked worktree. Skip to Step 3 (Project Setup). Do NOT create another worktree.3435Report with branch state:36- On a branch: "Already in isolated workspace at `<path>` on branch `<name>`."37- Detached HEAD: "Already in isolated workspace at `<path>` (detached HEAD, externally managed). Branch creation needed at finish time."3839**If `GIT_DIR == GIT_COMMON` (or in a submodule):** You are in a normal repo checkout.4041Has the user already indicated their worktree preference in your instructions? If not, ask for consent before creating a worktree:4243> "Would you like me to set up an isolated worktree? It protects your current branch from changes."4445Honor any existing declared preference without asking. If the user declines consent, work in place and skip to Step 3.4647## Step 1: Create Isolated Workspace4849**You have two mechanisms. Try them in this order.**5051### 1a. Native Worktree Tools (preferred)5253The 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 3.5455Native 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.5657Only proceed to Step 1b if you have no native worktree tool available.5859### 1b. Git Worktree Fallback6061**Only use this if Step 1a does not apply** — you have no native worktree tool available. Create a worktree manually using git.6263#### Directory Selection6465Follow this priority order. Explicit user preference always beats observed filesystem state.66671. **Check your instructions for a declared worktree directory preference.** If the user has already specified one, use it without asking.68692. **Check for an existing project-local worktree directory:**70```bash71ls -d .worktrees 2>/dev/null # Preferred (hidden)72ls -d worktrees 2>/dev/null # Alternative73```74If found, use it. If both exist, `.worktrees` wins.75763. **Check for an existing global directory:**77```bash78project=$(basename "$(git rev-parse --show-toplevel)")79ls -d ~/.config/superpowers/worktrees/$project 2>/dev/null80```81If found, use it (backward compatibility with legacy global path).82834. **If there is no other guidance available**, default to `.worktrees/` at the project root.8485#### Safety Verification (project-local directories only)8687**MUST verify directory is ignored before creating worktree:**8889```bash90git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null91```9293**If NOT ignored:** Add to .gitignore, commit the change, then proceed.9495**Why critical:** Prevents accidentally committing worktree contents to repository.9697Global directories (`~/.config/superpowers/worktrees/`) need no verification.9899#### Create the Worktree100101```bash102project=$(basename "$(git rev-parse --show-toplevel)")103104# Determine path based on chosen location105# For project-local: path="$LOCATION/$BRANCH_NAME"106# For global: path="~/.config/superpowers/worktrees/$project/$BRANCH_NAME"107108git worktree add "$path" -b "$BRANCH_NAME"109cd "$path"110```111112**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.113114## Step 3: Project Setup115116Auto-detect and run appropriate setup:117118```bash119# Node.js120if [ -f package.json ]; then npm install; fi121122# Rust123if [ -f Cargo.toml ]; then cargo build; fi124125# Python126if [ -f requirements.txt ]; then pip install -r requirements.txt; fi127if [ -f pyproject.toml ]; then poetry install; fi128129# Go130if [ -f go.mod ]; then go mod download; fi131```132133## Step 4: Verify Clean Baseline134135Run tests to ensure workspace starts clean:136137```bash138# Use project-appropriate command139npm test / cargo test / pytest / go test ./...140```141142**If tests fail:** Report failures, ask whether to proceed or investigate.143144**If tests pass:** Report ready.145146### Report147148```149Worktree ready at <full-path>150Tests passing (<N> tests, 0 failures)151Ready to implement <feature-name>152```153154## Quick Reference155156| Situation | Action |157|-----------|--------|158| Already in linked worktree | Skip creation (Step 0) |159| In a submodule | Treat as normal repo (Step 0 guard) |160| Native worktree tool available | Use it (Step 1a) |161| No native tool | Git worktree fallback (Step 1b) |162| `.worktrees/` exists | Use it (verify ignored) |163| `worktrees/` exists | Use it (verify ignored) |164| Both exist | Use `.worktrees/` |165| Neither exists | Check instruction file, then default `.worktrees/` |166| Global path exists | Use it (backward compat) |167| Directory not ignored | Add to .gitignore + commit |168| Permission error on create | Sandbox fallback, work in place |169| Tests fail during baseline | Report failures + ask |170| No package.json/Cargo.toml | Skip dependency install |171172## Common Mistakes173174### Fighting the harness175176- **Problem:** Using `git worktree add` when the platform already provides isolation177- **Fix:** Step 0 detects existing isolation. Step 1a defers to native tools.178179### Skipping detection180181- **Problem:** Creating a nested worktree inside an existing one182- **Fix:** Always run Step 0 before creating anything183184### Skipping ignore verification185186- **Problem:** Worktree contents get tracked, pollute git status187- **Fix:** Always use `git check-ignore` before creating project-local worktree188189### Assuming directory location190191- **Problem:** Creates inconsistency, violates project conventions192- **Fix:** Follow priority: existing > global legacy > instruction file > default193194### Proceeding with failing tests195196- **Problem:** Can't distinguish new bugs from pre-existing issues197- **Fix:** Report failures, get explicit permission to proceed198199## Red Flags200201**Never:**202- Create a worktree when Step 0 detects existing isolation203- Use `git worktree add` when you have a native worktree tool (e.g., `EnterWorktree`). This is the #1 mistake — if you have it, use it.204- Skip Step 1a by jumping straight to Step 1b's git commands205- Create worktree without verifying it's ignored (project-local)206- Skip baseline test verification207- Proceed with failing tests without asking208209**Always:**210- Run Step 0 detection first211- Prefer native tools over git fallback212- Follow directory priority: existing > global legacy > instruction file > default213- Verify directory is ignored for project-local214- Auto-detect and run project setup215- Verify clean test baseline216