Git Worktrees allow multiple agents (or developers) to work on different branches simultaneously in separate directories, all from the same repository. This eliminates merge conflicts during active development and enables true parallel work.
Git worktrees are a built-in Git feature that Claude Code can leverage for parallel agent execution with perfect isolation.
cd worktree-frontend/# Work on UI# Edit src/components/Profile.tsxgit add src/components/Profile.tsxgit commit -m "Add profile component"# Run tests in isolationnpm test# Push when readygit push -u origin feature/ui
cd worktree-backend/# Work on API# Edit src/api/profile.tsgit add src/api/profile.tsgit commit -m "Add profile API"# Run tests in isolationnpm test# Push when readygit push -u origin feature/api
cd worktree-tests/# Work on tests# Edit tests/profile.test.tsgit add tests/profile.test.tsgit commit -m "Add profile tests"# Run full test suitenpm test# Push when readygit push -u origin feature/tests
Use the isolation: worktree frontmatter to automatically create worktrees:
.claude/agents/frontend-specialist.md
---name: frontend-specialistdescription: PROACTIVELY handle React and TypeScript workmodel: opusisolation: worktreecolor: blue---# Frontend SpecialistYou work on UI components in complete isolation via git worktree.## Your WorkspaceYou're in a dedicated worktree directory. You can:- Commit without conflicts- Run tests independently- Push your branch when ready## Workflow1. Make changes to components2. Commit frequently3. Run tests: `npm test`4. Push when phase complete
When you invoke this agent via Task tool, Claude Code automatically creates a worktree on a new branch.
# Set up isolated environmentsgit worktree add -b feature/profile-ui worktree-uigit worktree add -b feature/profile-api worktree-apigit worktree add -b feature/profile-tests worktree-tests
2
Launch Agents in Worktrees
# In main directory, launch agents# Each agent's workdir points to its worktreeTask( subagent_type="frontend-specialist", description="Implement UI in worktree-ui", prompt="Work in worktree-ui/ directory. Implement profile UI.")Task( subagent_type="backend-specialist", description="Implement API in worktree-api", prompt="Work in worktree-api/ directory. Implement profile API.")
3
Merge When Ready
# After agents complete and tests passgit checkout maingit merge feature/profile-apigit merge feature/profile-uigit merge feature/profile-tests
You are working in worktree-reviews-ui/ directory.Tasks:1. Create ReviewList component2. Create ReviewForm component3. Add StarRating component4. Implement review submissionYour workspace:- Branch: reviews/frontend- Directory: worktree-reviews-ui/- Focus: src/components/reviews/Workflow:1. Implement each component2. Commit after each task3. Run `npm test` to verify4. Push branch when complete
Agent Prompt
You are working in worktree-reviews-api/ directory.Tasks:1. Create Review model2. Add POST /api/reviews endpoint3. Add GET /api/reviews/:productId endpoint4. Implement review validationYour workspace:- Branch: reviews/backend- Directory: worktree-reviews-api/- Focus: src/api/reviews/Workflow:1. Implement each endpoint2. Commit after each task3. Run tests: `npm test`4. Push branch when complete
Agent Prompt
You are working in worktree-reviews-tests/ directory.Tasks:1. Unit tests for ReviewList, ReviewForm2. Unit tests for review API3. Integration tests for review flow4. E2E test for submit and displayYour workspace:- Branch: reviews/tests- Directory: worktree-reviews-tests/- Focus: tests/reviews/Note: You're testing against mocked API contracts.Actual integration happens after merge.
Problem: Want to see frontend changes while backend agent runsSolution: Run dev servers from each worktree
# Terminal 1: Frontend dev servercd worktree-frontend/npm run dev # Port 3000# Terminal 2: Backend dev servercd worktree-backend/npm run dev # Port 4000# Terminal 3: Run testscd worktree-tests/npm test -- --watch
Problem: Need to test frontend + backend together before mergingSolution: Temporary integration worktree
# Create integration worktreegit worktree add -b integration/reviews worktree-integrationcd worktree-integration/# Merge both branchesgit merge reviews/frontendgit merge reviews/backend# Run full integration testsnpm test# If tests pass, merge both into main# If tests fail, fix in respective worktrees
Problem: Need to pull latest changes from another branchSolution: Fetch + merge in worktree
# In worktree-frontend/ (on reviews/frontend branch)cd worktree-frontend/# Pull latest from backend to sync typesgit fetch origin reviews/backendgit merge origin/reviews/backend# Resolve any conflicts# Continue frontend work
# Work on multiple features simultaneouslygit worktree add -b feature/auth worktree-authgit worktree add -b feature/payments worktree-paymentsgit worktree add -b feature/notifications worktree-notifications# Each agent/human works independently# Merge features as they complete
# Main worktree: developing feature# Urgent bug reported# Create hotfix worktree from maingit worktree add -b hotfix/critical-bug worktree-hotfixcd worktree-hotfix/# Fix bug, test, merge immediately# Continue feature work in main worktree
# Create worktree to review PRgit worktree add worktree-pr-review pr/123cd worktree-pr-review/npm installnpm test# Review, test, comment# Remove when done
# Try risky refactoring without affecting main workgit worktree add -b experiment/new-architecture worktree-experiment# Experiment freely# If it works: merge# If it fails: delete worktree, no harm done
# Each agent gets isolated workspacegit worktree add -b agent/frontend worktree-agent-1git worktree add -b agent/backend worktree-agent-2git worktree add -b agent/tests worktree-agent-3# Agents work in parallel# Zero conflicts during development
Problem: Trying to check out a branch that’s already active in another worktreeSolution:
# List active worktreesgit worktree list# Either:# 1. Use existing worktree for that branch# 2. Create new branch for new worktreegit worktree add -b feature/ui-v2 worktree-ui
Worktree out of sync
Problem: Worktree shows old commitsSolution:
cd worktree-frontend/# Pull latestgit pull# Or fetch + merge specific branchgit fetch origingit merge origin/feature/ui
Can't remove worktree
Problem: git worktree remove failsSolution:
# Force removegit worktree remove --force worktree-frontend# Or manually delete and prunerm -rf worktree-frontend/git worktree prune
Lost worktree location
Problem: Forgot where worktrees areSolution:
# List all worktrees with pathsgit worktree list# Output shows full paths:# /Users/dev/project abc1234 [main]# /Users/dev/project/worktree-ui def5678 [feature/ui]