Blog

Three agents in parallel without collisions: Claude Code worktrees step by step

With claude --worktree each session works in its own folder and branch. How to set it up, copy your .env over and isolate subagents without conflicts.

If you want several Claude Code sessions working on the same repo at once, give each one its own worktree with claude --worktree <name>. It is the simplest way to have one fix a bug while another builds a feature without stepping on each other’s files.

What a worktree is

A git worktree is a second working folder for the same repository. It has its own files and its own branch, but shares history and remote with your main checkout. It is not a clone: it does not duplicate .git and you do not git pull in two places.

The problem it solves is specific. Open three terminals running Claude Code in the same folder and all three agents edit the same files. One changes auth.ts while another runs the tests halfway through that change, and you no longer know what broke what. With one worktree per session, each agent has its own copy and branch, and you bring the work together at the end with ordinary pull requests.

How to use it

1. Start each session in its own worktree

claude --worktree fix-login

According to the Claude Code documentation, this creates .claude/worktrees/fix-login/ at the repo root, on a new branch called worktree-fix-login, and starts Claude inside it. In another terminal, repeat with another name:

claude -w new-invoice
claude -w bump-dependencies

Three sessions, three folders, three branches. If you leave out the name, Claude makes one up.

By default the branch starts from the remote’s default branch (usually main), not from whatever you have half done locally. To start from your current work, set "worktree": { "baseRef": "head" } in your settings.

2. Ignore the folder in git

Add this to your .gitignore so worktree contents do not show up as untracked files in the main checkout:

.claude/worktrees/

3. Copy your .env with .worktreeinclude

A worktree is a clean checkout, so it does not bring ignored files like .env. Without them the agent cannot start the project or pass the tests. The fix is a .worktreeinclude file at the root, using .gitignore syntax:

.env
.env.local
config/secrets.json

It only copies files that match a pattern and are also ignored by git, so it never duplicates tracked files. You still need to install dependencies in each worktree (npm install or whatever your project uses).

4. Isolate subagents

If you use subagents for mechanical work, you can make each one always run in its own worktree by adding isolation: worktree to its definition in .claude/agents/:

---
name: refactorer
description: Applies mechanical refactors across many files
isolation: worktree
---

Apply the requested refactor across every affected file, then run the tests
and report the results.

If the subagent finishes without changes, Claude Code removes its worktree automatically.

5. Clean up on exit

When you close an interactive session, Claude checks the worktree. If it is clean and the session is unnamed, it removes the worktree and its branch. If there are changes or commits, it asks whether to keep it. With claude -p (non-interactive) there is no prompt and no cleanup: remove them yourself with git worktree remove.

What Claude Code blocks inside a worktree

This is the part I find most interesting. While a session is isolated in a worktree, Claude Code applies four checks:

  • It blocks file edits that target the main checkout.
  • It blocks commands whose working directory is the main checkout.
  • It blocks commands that redirect git into the main checkout (git -C, GIT_DIR, a cd before git).
  • It blocks shell commands when it cannot verify that the git they run stays inside the worktree.

In other words, isolation does not depend on the agent behaving. If it tries to touch your main folder, the tool stops it and tells it why.

My take

If you work alone with agents, this is the first thing I would set up. The temptation is to open several sessions in the same folder because it is faster, and that works until two agents touch the same file. A worktree per task costs one command, not an afternoon untangling mixed changes.

My practical advice to get started:

  1. One worktree per task, with a name that says what it does (fix-login, not test2).
  2. .worktreeinclude from day one, or every new session will trip on the same missing file.
  3. Every worktree ends in a pull request. That way you review each agent’s work on its own, not one giant diff with everything mixed together.

The real limit is not the tool: it is your capacity to review. Three agents in parallel produce three pull requests someone has to read. I would not go past two or three at a time until the tests are good enough to trust without reading every line.

Sources

If practical guides like this help you, follow me on LinkedIn, where I share how I work with AI agents.

← All articles

Have a product to build or a tech stack to put in order?

Tell me what you are trying to achieve and where you are today. I reply personally, usually within a day.

Free · 30 minutes

Digital strategy session

30 minutes to map where to start digitalizing and transforming your business. You pay nothing, before or after.

When you book, you answer three short questions: what you need, what stage your project is at and when. That way the session starts with context.