Overview
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.
The Problem with Shared Branches
When multiple agents work on the same branch:❌ Traditional Approach
Single Working Directory
- Agent A modifies
file.ts - Agent B also modifies
file.ts - Merge conflict on every commit
- Constant pull/push coordination needed
- Build breaks if agents aren’t synced
✅ Git Worktrees
Isolated Directories
- Agent A:
worktree-a/file.ts - Agent B:
worktree-b/file.ts - Zero conflicts during development
- Each can run tests independently
- Merge only when ready
How Git Worktrees Work
Key Concepts
Main Repository
Main Repository
Your original
.git directory and working tree. This is the “main” worktree.Additional Worktrees
Additional Worktrees
Separate directories linked to the same
.git, each on a different branch.Basic Usage
Creating Worktrees
Working in Worktrees
- Agent A (Frontend)
- Agent B (Backend)
- Agent C (Tests)
Managing Worktrees
Claude Code Integration
Use worktrees with Claude Code agents for parallel development:Pattern 1: Agent Isolation Frontmatter
Use theisolation: worktree frontmatter to automatically create worktrees:
.claude/agents/frontend-specialist.md
When you invoke this agent via Task tool, Claude Code automatically creates a worktree on a new branch.
Pattern 2: Manual Worktree Setup
Create worktrees before launching agents:1
Create Worktrees
2
Launch Agents in Worktrees
3
Merge When Ready
Real-World Example: E-commerce Feature
Implementing a product review system with 3 parallel agents:Setup
Agent Execution
- Frontend Agent
- Backend Agent
- Testing Agent
Agent Prompt
Merging Strategy
Advanced Patterns
Shared Dependencies
Problem:node_modules duplicated in each worktree
Solution: Symlink shared directories
Hot Reloading Across Worktrees
Problem: Want to see frontend changes while backend agent runs Solution: Run dev servers from each worktreeIntegration Testing Across Worktrees
Problem: Need to test frontend + backend together before merging Solution: Temporary integration worktreeSyncing Changes Between Worktrees
Problem: Need to pull latest changes from another branch Solution: Fetch + merge in worktreeBest Practices
Do create worktrees for independent workstreams
Do use descriptive worktree directory names
Do clean up worktrees when done
Do commit frequently in each worktree
Do run tests in each worktree before merging
Don’t modify the same file in multiple worktrees simultaneously
Don’t forget to push branches before removing worktrees
Don’t check out the same branch in multiple worktrees (Git prevents this)
Workflows with Worktrees
Boris Cherny’s 5 Patterns
From Boris’s tweet:- 1. Parallel Features
- 2. Hotfix While Developing
- 3. Review PRs
- 4. Experiment Safely
- 5. Agent Teams
Troubleshooting
Error: 'branch is already checked out'
Error: 'branch is already checked out'
Problem: Trying to check out a branch that’s already active in another worktreeSolution:
Worktree out of sync
Worktree out of sync
Problem: Worktree shows old commitsSolution:
Can't remove worktree
Can't remove worktree
Problem:
git worktree remove failsSolution:Lost worktree location
Lost worktree location
Problem: Forgot where worktrees areSolution:
Comparison with Alternatives
Related Patterns
Agent Teams
Multiple agents working in parallel - perfect with worktrees
RPI Workflow
Phased implementation that can leverage worktrees
Orchestration Workflow
Command → Agent → Skill pattern
Resources
Git Worktree Docs
Official Git documentation
Boris Cherny's Guide
Real-world worktree patterns
Claude Code Isolation
Agent isolation documentation
Best Practices Repo
Community examples and patterns
