Use when implementing features in git worktrees to ensure all changes stay in the correct worktree - prevents "bleeding" of changes back to main branch
Prevents git changes from bleeding between worktrees by enforcing verification protocols and chained commands. Use when implementing features in isolated git worktrees to ensure all operations stay in the correct workspace and don't affect the main branch.
/plugin marketplace add samjhecht/wrangler/plugin install wrangler@samjhecht-pluginsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
MANDATORY: When using this skill, announce it at the start with:
Using Skill: worktree-isolation | [brief purpose based on context]
Git worktrees provide isolated workspaces, but Claude Code has characteristics that can cause changes to "bleed" to the wrong worktree:
This skill provides protocols to ensure ALL changes stay in the intended worktree.
# WRONG - This DOES NOT work in Claude Code
cd /path/to/worktree
git status # This runs in ORIGINAL directory, not worktree!
Each Bash tool call is independent. The cd command affects only that shell session, which ends immediately. The next Bash call starts fresh in the original working directory.
When you spawn a subagent with:
Work from: /path/to/worktree
This is just text. The subagent has no verified context about which directory it should use. If it runs git commit, it commits wherever Claude Code's process is running - likely your main branch.
WRONG:
cd /path/to/worktree
git add .
git commit -m "message"
RIGHT:
cd /path/to/worktree && git add . && git commit -m "message"
Or use git's -C flag:
git -C /path/to/worktree add .
git -C /path/to/worktree commit -m "message"
Before ANY git write operation (add, commit, push, merge, etc.), run verification:
cd /path/to/worktree && \
echo "=== WORKTREE VERIFICATION ===" && \
echo "Current directory: $(pwd)" && \
echo "Git root: $(git rev-parse --show-toplevel)" && \
echo "Branch: $(git branch --show-current)" && \
echo "Expected: /path/to/worktree on branch-name" && \
echo "============================"
STOP if verification fails. Do not proceed with git operations.
Never use relative paths in worktree contexts:
WRONG:
git add src/file.ts
RIGHT:
git -C /absolute/path/to/worktree add src/file.ts
When dispatching subagents for worktree work, include this verification block:
## CRITICAL: Working Directory Verification
You are working in worktree: /absolute/path/to/worktree
Expected branch: feature-branch-name
**BEFORE making ANY changes, run this verification:**
```bash
cd /absolute/path/to/worktree && \
echo "VERIFICATION:" && \
echo "Directory: $(pwd)" && \
echo "Git root: $(git rev-parse --show-toplevel)" && \
echo "Branch: $(git branch --show-current)"
Expected output:
If ANY value doesn't match, STOP immediately and report the mismatch.
For ALL git commands, use this pattern:
cd /absolute/path/to/worktree && git [command]
## Implementation Pattern
### Setting Up a Worktree for Implementation
```bash
# 1. Create the worktree
WORKTREE_PATH="$(pwd)/.worktrees/feature-name"
BRANCH_NAME="feature/feature-name"
git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME"
# 2. Verify creation
git worktree list | grep "$WORKTREE_PATH"
# 3. Install dependencies (chained!)
cd "$WORKTREE_PATH" && npm install && npm test
# 4. Store the absolute path for subagents
echo "Worktree ready at: $WORKTREE_PATH"
When using the Task tool for work in a worktree:
Tool: Task
Description: "Implement feature X in worktree"
Prompt: |
You are implementing [task] in an isolated git worktree.
## CRITICAL: Working Directory Context
**Worktree location:** /Users/sam/project/.worktrees/feature-name
**Branch:** feature/feature-name
**Main repo:** /Users/sam/project (DO NOT modify)
### Verification (RUN FIRST)
Before ANY work, verify your location:
```bash
cd /Users/sam/project/.worktrees/feature-name && \
echo "Directory: $(pwd)" && \
echo "Git root: $(git rev-parse --show-toplevel)" && \
echo "Branch: $(git branch --show-current)"
Expected:
If verification fails, STOP and report. Do not proceed.
ALL commands must use this pattern:
cd /Users/sam/project/.worktrees/feature-name && [your command]
Examples:
cd /Users/sam/project/.worktrees/feature-name && npm testcd /Users/sam/project/.worktrees/feature-name && git statuscd /Users/sam/project/.worktrees/feature-name && git add . && git commit -m "message"[Task description here]
Include in your report:
### Verifying Subagent Work
After a subagent completes, verify their work landed in the right place:
```bash
# Check the worktree has the commits
cd /path/to/worktree && git log --oneline -5
# Verify main branch was NOT modified
cd /path/to/main/repo && git log --oneline -5
# Should NOT contain subagent's commits
Problem: Assuming subagent is in the right directory Solution: Always include explicit verification in subagent prompts
Problem:
cd /worktree
git status # WRONG - runs in original directory
Solution:
cd /worktree && git status # RIGHT - chained
Problem:
git add src/file.ts # Where does this run?
Solution:
git -C /absolute/worktree/path add src/file.ts
Problem: Work from: /path/to/worktree is just documentation
Solution: Include verification commands and expected output in prompt
When using the implement skill with worktrees:
Before dispatching any implementation subagents:
# Store worktree path as absolute
WORKTREE_PATH="$(cd /path/to/worktree && pwd)"
echo "Using worktree: $WORKTREE_PATH"
# Verify it's actually a worktree
git -C "$WORKTREE_PATH" rev-parse --is-inside-work-tree
git -C "$WORKTREE_PATH" worktree list | grep "$WORKTREE_PATH"
Replace the generic Work from: [current directory] with:
## Working Directory (CRITICAL)
**Worktree:** [ABSOLUTE_WORKTREE_PATH]
**Branch:** [BRANCH_NAME]
### Mandatory Verification (run before any work)
```bash
cd [ABSOLUTE_WORKTREE_PATH] && \
echo "Location: $(pwd)" && \
echo "Branch: $(git branch --show-current)" && \
test "$(pwd)" = "[ABSOLUTE_WORKTREE_PATH]" && \
echo "VERIFIED" || echo "VERIFICATION FAILED - STOP"
All commands MUST follow this pattern:
cd [ABSOLUTE_WORKTREE_PATH] && [command]
DO NOT run any git commands without the cd && prefix.
### Completion Verification
After all subagents complete, verify isolation was maintained:
```bash
# 1. Check worktree has expected commits
echo "=== Worktree commits ===" && \
cd /path/to/worktree && git log --oneline -10
# 2. Check main branch was NOT modified
echo "=== Main branch commits (should be unchanged) ===" && \
cd /path/to/main && git log --oneline -5
# 3. Verify no uncommitted changes in main
cd /path/to/main && git status --short
| Scenario | Pattern |
|---|---|
| Single git command | git -C /worktree/path [command] |
| Multiple commands | cd /worktree/path && cmd1 && cmd2 |
| Subagent dispatch | Include verification block + expected output |
| After subagent returns | Verify commits in worktree, not main |
| Any uncertainty | Run verification before proceeding |
STOP if you see:
-C flag or cd && prefixALWAYS:
&&Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.