Run parallel Claude Code sessions without them stepping on each other
Worktrees stop two sessions editing the same files. They don't decide which tasks are safe to run side by side, or what to do at merge time. The flag, the gotchas, a driver script and a merge routine.

Two Claude Code sessions in the same checkout will eventually edit the same file. Git worktrees are the standard answer: each session gets its own working directory and branch, backed by the same repository history. Claude Code has a flag for it, and the worktree docs cover more edge cases than the one-liner suggests.
The flag solves file collisions. It doesn’t solve the two harder parts of running agents in parallel: picking tasks that are safe to run together, and merging what comes back. Most of this piece is about those.
Start one
claude --worktree feature-auth
-w works too. By default the worktree lands in .claude/worktrees/feature-auth/ at your repo root, on a new branch called worktree-feature-auth. Run the command again with another name in a second terminal and you have two isolated sessions. Skip the name and Claude invents one, like bright-running-fox.
Add .claude/worktrees/ to your .gitignore, or the contents show up as untracked files in your main checkout.
Three things a fresh checkout doesn’t have
A worktree starts clean, so it’s missing everything git doesn’t track.
Dependencies. Ask Claude to run your install step, or do it yourself in the worktree directory. Nothing carries over from the main checkout’s node_modules or virtualenv.
Local config. Your .env isn’t there either. With .env in .gitignore, a fresh git worktree add gives a directory holding the tracked files and nothing else. Add a .worktreeinclude file at the project root, in .gitignore syntax, listing the ignored files to copy into every new worktree:
.env
.env.local
config/secrets.json
Only files that match a pattern and are gitignored get copied, so tracked files are never duplicated.
The right starting point. New worktrees branch from the repository’s default branch on the remote, not from whatever you have checked out. If you want a worktree to carry your unpushed commits, set worktree.baseRef to "head" in settings. Branching from a pull request is a one-liner, and the quotes matter so your shell doesn’t read # as a comment:
claude --worktree "#1234"
Pick tasks that can actually run in parallel
Isolation keeps the files apart while the work happens. It doesn’t keep the changes apart when you merge. Two sessions that both edit the same function will each be correct in their own worktree and produce a conflict in yours.
A short test before you start a second session: can you list the files each task touches, and would the lists overlap? Good candidates:
- A bug fix in one module and a feature in another.
- Writing tests for module A and docs for module B.
- A mechanical refactor in a directory nobody else is touching.
Bad candidates: two features that both add a field to the same model, two migrations, anything that renames a shared type. Run those one after the other, or have one session finish and merge first.
A collision looks like this. Create two worktrees and have each change the same line of one file. Merging the first branch is fine. Merging the second gives:
Auto-merging shared.txt
CONFLICT (content): Merge conflict in shared.txt
Automatic merge failed; fix conflicts and then commit the result.
That is the ordinary git conflict you’d get from two people, and you resolve it the same way. The cost is that neither agent knew the other existed, so neither can reconcile them. The cheapest fix is to avoid the overlap up front.
Run several without opening several terminals
For a handful of independent tasks, a small driver script is enough. Each task gets a name, which becomes the worktree and branch. The -p flag runs Claude non-interactively, and the docs show --allowedTools and --permission-mode dontAsk for unattended runs, so anything you didn’t pre-approve is denied and not left waiting for a prompt:
#!/usr/bin/env bash
set -u
mkdir -p logs
declare -A TASKS=(
[fix-date-parse]="Fix the date parsing bug described in issues/142.md. Add a regression test."
[docs-cli-flags]="Update docs/cli.md so it lists every flag in src/cli.ts. Do not touch source files."
[tests-billing]="Add unit tests for src/billing/proration.ts covering the edge cases in the file's comments."
)
for name in "${!TASKS[@]}"; do
claude -p --worktree "$name" "${TASKS[$name]} Commit your work when the tests pass." \
--allowedTools "Read,Edit,Bash(git add *),Bash(git commit *),Bash(npm test *)" \
--permission-mode dontAsk > "logs/$name.txt" 2>&1 &
done
wait
git worktree list
The associative array needs bash 4 or newer, which the macOS default shell isn’t. This script hasn’t been run against the real CLI. The flags come from the docs and from claude --help, where -w, --worktree and --allowedTools are present on 2.1.291. Test it with one task before you trust three. Also check two things the docs say about -p: a non-interactive run skips the workspace trust prompt, and it doesn’t clean up its worktree when it ends. Both matter when you’re running unattended.
Three tasks in parallel is a sensible ceiling for one person who has to review the results. Claude Code also has a /batch command that splits a change across 5 to 30 subagents, each in its own worktree. That’s a different tool, for one large mechanical change.
Merge what comes back
When the runs finish, git worktree list shows each path, commit and branch. From the main checkout:
git log --oneline main..worktree-fix-date-parse # what did it do?
git diff main...worktree-fix-date-parse --stat # how big is it?
git merge worktree-fix-date-parse
Two worktrees that edit different files merge back without a conflict, and the log shows both commits plus the merge. Review each branch the way you’d review a colleague’s: read the diff, run the tests on the merged result, not only inside each worktree. A branch that passes alone can still fail after the other lands.
For a stronger check, use the Writer and Reviewer pattern from the best-practices guide: a second session, with fresh context, reviews the first one’s branch. It sees only the diff, so it isn’t biased toward code it just wrote.
Subagents can get their own worktree
If a subagent edits files in parallel with others inside one session, give it isolation in its frontmatter. This example is from the docs:
---
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.
Each run gets a temporary worktree, removed automatically if the subagent finishes without changes. One catch from the docs: a subagent in its own worktree takes its starting instructions from your main conversation, and doesn’t load the CLAUDE.md at the worktree’s root, even if that file differs on the worktree’s branch.
Cleanup
When you exit an interactive session, Claude checks the worktree for uncommitted files and new commits. A clean, unnamed worktree is removed along with its branch. A named one asks first. If there’s work in it, you choose between keeping and removing, and removing deletes the branch too.
Non-interactive runs with -p don’t get an exit prompt, so they leave their worktrees behind. Remove them with git worktree remove, and if git refuses because of a lock, run git worktree unlock first. After git worktree remove and git branch -d, git worktree list should show only the main checkout, which is a sensible end-of-day check.
Isolation and permissions
While a session is in a worktree, Claude Code blocks edits to paths in the main checkout, and blocks commands that point git back at it. That’s a safety net against an agent wandering out of its sandbox. It also means a clever one-liner with computed command names may get refused, and the error tells Claude how to rewrite it. The docs are clear on the limit: the checks read edit paths, working directories and command text, and don’t track what a shell command writes. A cp into the main checkout isn’t stopped by them.
Approvals are shared. If you pick “Yes, and don’t ask again” for a Bash command inside a worktree, the rule is saved to the main checkout’s .claude/settings.local.json, so it applies everywhere in that repo, including after the worktree is gone. Worth knowing before you click it.
Try it before lunch
- Add
.claude/worktrees/to.gitignoreand a.worktreeincludelisting your.envfiles. - Pick two tasks whose file lists don’t overlap. Say them out loud: if you can’t name the files, they probably overlap.
- Start both with
claude --worktree <name>in two terminals. - Merge the first, run the tests, merge the second, run the tests again.
- Run
git worktree list. It should show only the main checkout. Clean up whatever else is there.