Claude Code Git Worktrees

How to use git worktrees with Claude Code to run multiple parallel, isolated coding sessions on different branches simultaneously. Setup, patterns, and best practices for parallel AI-assisted development.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Git worktrees let you check out multiple branches simultaneously in separate directories. Combined with Claude Code, they enable parallel AI-assisted development — one session fixing a bug on main while another builds a feature on a separate branch, with zero interference.

Setup: create a worktree

# Create a worktree for a new feature branch
git worktree add ../my-project-feature feature/payment-v2

# Or create the branch and worktree together
git worktree add -b feature/payment-v2 ../my-project-feature

# List all worktrees
git worktree list

You now have two directories:

Run Claude Code in each worktree

# Terminal 1 — main branch
cd ~/my-project
claude "Fix the null pointer in OrderProcessor.process()"

# Terminal 2 — feature branch (simultaneously)
cd ~/my-project-feature
claude "Build the PaymentGateway interface and Stripe implementation"

Both sessions run concurrently. Changes in one directory have no effect on the other.

Typical parallel development patterns

PatternWorktree 1Worktree 2
Hotfix + featureFix critical bug on mainContinue feature work uninterrupted
Parallel featuresFeature A (auth refactor)Feature B (new API endpoints)
Review + continueAddress PR feedback on feature branchStart next feature
ExperimentStable implementationRisky refactor — easy to abandon
Multi-agent pipelineAgent writes implementationAgent writes tests (different branch)

Sharing CLAUDE.md across worktrees

Create CLAUDE.md in the repo root once. Each worktree inherits it automatically (it's part of the git history). No symlinks needed — git tracks it like any other file.

# CLAUDE.md lives in the repo root
ls ~/my-project/CLAUDE.md        # exists
ls ~/my-project-feature/CLAUDE.md  # also exists (same file, different worktree)

Worktrees in Claude Code's own subagent system

Claude Code uses the worktree pattern internally when spawning subagents with the isolation: "worktree" option. Each subagent gets an isolated copy of the repo on a temporary branch, does its work, and the results are merged back. You can replicate this pattern in your own orchestration:

import subprocess, os

def claude_in_worktree(branch: str, prompt: str, base_dir: str) -> str:
    worktree_path = f"/tmp/wt-{branch}"
    subprocess.run(["git", "worktree", "add", "-b", branch, worktree_path], cwd=base_dir)
    try:
        result = subprocess.run(
            ["claude", "--print", prompt],
            cwd=worktree_path,
            capture_output=True, text=True
        )
        return result.stdout
    finally:
        subprocess.run(["git", "worktree", "remove", "--force", worktree_path], cwd=base_dir)
        subprocess.run(["git", "branch", "-D", branch], cwd=base_dir)

Cleanup

# Remove a worktree when done
git worktree remove ../my-project-feature

# Force remove (if Claude Code left uncommitted files)
git worktree remove --force ../my-project-feature

# Prune stale worktree references
git worktree prune

Worktrees vs. Docker containers for isolation

ConcernWorktreesDocker containers
Setup time<1 second10–60 seconds (image pull/build)
Disk overheadWorking files only (shared .git)Full OS layer + dependencies
File isolationPer-branch file isolationFull filesystem isolation
Network isolationNoneYes (separate network namespace)
Best forCode isolation across branchesFull environment isolation

For running Claude Code without any interactive session (batch pipelines, CI), see Claude Code headless mode. For coordinating multiple Claude Code agents programmatically, see Claude Code multi-agent patterns.

Frequently asked questions

Can I run multiple Claude Code sessions at the same time?
Yes — use git worktrees. Each worktree is a separate working directory pointing to a different branch. Claude Code in worktree A won't see or interfere with Claude Code in worktree B. Both share the same .git history.
What is a git worktree?
A git worktree is an additional working directory linked to the same repository. You can have worktree A on `main` and worktree B on `feature/auth` simultaneously — both checked out, both editable, no stash required.
Why use worktrees instead of just opening two terminals?
Two terminals in the same directory both share the same working tree. Edits in one terminal are immediately visible in the other — Claude Code sessions would step on each other. Worktrees give each session a fully isolated file system view.
Do Claude Code sessions in different worktrees share context?
No — each is an independent session with its own conversation history and context window. CLAUDE.md at the repo root is shared (symlinked), but the active file states and conversation history are separate.
Are there limits to how many worktrees I can have?
No hard limit. Practical limit is RAM and disk — each worktree is a full copy of the working files (not the git objects, those are shared). For a 50MB project, 5 worktrees = ~250MB working-file footprint.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript