§04.06

Run Parallel Claude Code Sessions with Git Worktrees

Start a second Claude Code session in its own git worktree with `claude --worktree`, keep .env files in it, and clean up when you're done. Copy-paste.

published 06 Sept 2026 checked against docs 06 Sept 2026 3 min in Workflows Markdown

On this page6 sections
  1. 1. Start Claude in a worktree
  2. 2. Start a second session
  3. 3. Carry your .env into every worktree
  4. 4. Branch from a pull request instead
  5. 5. Clean up
  6. Worth knowing

Two Claude Code sessions editing the same checkout will step on each other’s files. A git worktree is a separate working directory on its own branch that shares the repository history — so one session can build a feature while another fixes a bug, and neither sees the other’s edits. Claude Code creates and cleans these up for you.

1. Start Claude in a worktree

From the root of a repository that has at least one commit:

claude --worktree feature-auth

Creates .claude/worktrees/feature-auth/ on a new branch worktree-feature-auth and starts Claude inside it. -w is the short flag; omit the name and Claude invents one like bright-running-fox.

The first interactive run in a directory needs workspace trust — if --worktree exits asking for it, run plain claude there once, accept the dialog, then retry.

2. Start a second session

In another terminal, same repository:

claude --worktree fix-login-bug

That’s the whole trick: each terminal owns a worktree, each worktree owns a branch. While a session is isolated, Claude Code blocks edits and git commands that would reach back into the main checkout, and tells Claude how to rewrite a refused command.

Keep the worktree folders out of git status:

echo ".claude/worktrees/" >> .gitignore

3. Carry your .env into every worktree

A worktree is a fresh checkout, so gitignored files such as .env aren’t there. List them in a .worktreeinclude file at the project root (gitignore syntax; only files that are also gitignored get copied):

.env
.env.local
config/secrets.json

Dependencies are not copied — ask Claude to run your install step, or run npm install / uv sync yourself inside .claude/worktrees/<name>/.

4. Branch from a pull request instead

Quote the argument so the shell doesn’t read # as a comment:

claude --worktree "#1234"

Fetches that PR’s head from origin and creates the worktree at .claude/worktrees/pr-1234. A GitHub PR URL or GitLab MR URL works too.

5. Clean up

When you exit an interactive worktree session, Claude checks for uncommitted work and new commits. A clean, unnamed session’s worktree is removed automatically; if there is work in it, you’re asked whether to keep or remove it. Non-interactive -p runs never prompt, so remove those yourself:

git worktree list
git worktree remove .claude/worktrees/feature-auth

--force removes a worktree that still has uncommitted changes; if git says the worktree is locked, run git worktree unlock <path> first.

Worth knowing

  • New worktrees branch from the repository’s default branch (main on the remote). To branch from your current local HEAD instead — say, to isolate a subagent on in-progress work — set {"worktree": {"baseRef": "head"}} in settings.json.
  • Reusing a name (claude --worktree feature-auth again) reopens the existing worktree rather than creating a new one.
  • Subagents can get their own worktrees: add isolation: worktree to a custom agent’s frontmatter in .claude/agents/.

Next: use subagents · run Claude Code headless in a script or CI.

← All Workflows plates · Search all guides

↑↓ move↵ openalt+↵ copy first command

Keyboard

⌘/ctrl+K or /
Search all guides
alt+↵
In search: copy the guide's first command
j / k
Move through a list of guides
c
On a guide: copy its first command
t
Toggle light / dark
?
This list