Skip to main content

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

Your original .git directory and working tree. This is the “main” worktree.
Separate directories linked to the same .git, each on a different branch.
All worktrees share the same commits, branches, and history. Changes in one worktree don’t affect others until merged.

Basic Usage

Creating Worktrees

Working in Worktrees

Managing Worktrees

Claude Code Integration

Use worktrees with Claude Code agents for parallel development:

Pattern 1: Agent Isolation Frontmatter

Use the isolation: 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

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 worktree

Integration Testing Across Worktrees

Problem: Need to test frontend + backend together before merging Solution: Temporary integration worktree

Syncing Changes Between Worktrees

Problem: Need to pull latest changes from another branch Solution: Fetch + merge in worktree

Best 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:

Troubleshooting

Problem: Trying to check out a branch that’s already active in another worktreeSolution:
Problem: Worktree shows old commitsSolution:
Problem: git worktree remove failsSolution:
Problem: Forgot where worktrees areSolution:

Comparison with Alternatives

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