| name | git-worktrees |
| description | Use when creating, modifying, or managing git worktrees, working on parallel feature branches, or isolating work from the main repository. |
Git Worktrees - Parallel Feature Development
Acceptance Checks
What are Git Worktrees?
Git worktrees allow multiple branches checked out simultaneously in different directories. Essential for:
- Working on multiple features in parallel
- Running multiple Claude instances on different features
- Keeping the main repository clean and stable
- Avoiding branch switching disruptions
Standard Worktree Pattern
Place worktrees in worktrees/<name>/ inside the repo.
Why Inside the Repo?
Agent harnesses (Claude Code, etc.) often reset cwd to the project root between commands when you cd to a sibling path outside the repo. That breaks multi-command workflows that assume persistent cwd. Placing worktrees at <repo>/worktrees/<name>/ keeps cwd stable because it's still inside the project root.
The old concern — "inside-repo worktrees freeze a stale CLAUDE.md" — applies to any worktree regardless of location (each worktree checks out its branch's version of CLAUDE.md). It's not a reason to prefer sibling directories.
Layout:
~/ws/my-project/
├── (main checkout)
└── worktrees/
├── feature-auth/
└── feature-api/
Add worktrees/ to .gitignore in the main repo so the directory itself is never committed.
Creating a Worktree
NEVER modify the main repository when creating a worktree (no git checkout, no git pull, no branch switching).
mkdir -p worktrees
git worktree add worktrees/feature-name -b feature/descriptive-name
git worktree add worktrees/feature-name existing-branch-name
git worktree add worktrees/feature-name -b feature/descriptive-name main
cd worktrees/feature-name
git pull origin main
MAIN_REPO="../.."
cp "$MAIN_REPO/.env" .env 2>/dev/null || true
find "$MAIN_REPO/apps" "$MAIN_REPO/packages" -maxdepth 2 \( -name ".env.local" -o -name ".env.*.local" \) 2>/dev/null | while read file; do
target="${file#$MAIN_REPO/}"
mkdir -p "$(dirname "$target")"
cp "$file" "$target" 2>/dev/null || true
done
pnpm install
Managing Worktrees
git worktree list
git worktree remove worktrees/feature-name
git worktree prune
Cleanup After PR Merge
git worktree remove worktrees/feature-name
git branch -d feature/descriptive-name
Critical Rules for Multi-Agent Environments
MULTIPLE AGENTS MAY BE WORKING SIMULTANEOUSLY
- NEVER modify the main repository when creating worktrees - No checkout, no pull, no branch switching
- NEVER stash changes on the main repository - Disrupts other agents' work
- ALWAYS use worktrees for feature development - Keep main repository clean
- NEVER checkout or pull in main repo - Create worktree first, then pull inside it
- COMMUNICATE through commits - Use clear commit messages
Rules for Main Repository Directory
- NEVER run
git checkout - disrupts active work
- NEVER run
git pull - may cause conflicts with uncommitted changes
- NEVER switch branches - use worktrees instead
- Don't make changes directly - use worktrees
- Don't stash changes - affects all agents
- Create worktrees:
git worktree add worktrees/<name> -b <branch-name>
Working with Issues
git worktree add worktrees/issue-{number} -b feature/issue-{number}-brief-description
cd worktrees/issue-{number}
git fetch origin main
git merge origin/main
cp ../../.env.local .env.local 2>/dev/null || true
pnpm i
Task Tracking
When creating a worktree, add it to the TODO list:
{
"content": "Working in worktree: worktrees/feature-name (branch: feature/branch-name)",
"status": "in_progress",
"activeForm": "Working in worktree feature-name"
}
When done:
{
"content": "Clean up worktree: worktrees/feature-name",
"status": "completed"
}
When to Use Worktrees
Use when:
- User asks to work on a new feature
- Multiple features need parallel development
- Testing changes without disrupting main branch
- Running multiple Claude instances on different features
- User explicitly mentions worktrees or parallel work
Don't use when:
- Making quick fixes on current branch
- User hasn't requested parallel development
- Working on a single feature already checked out