#12: Parallel Sessions with Worktrees 🌳
Today we'll look at running several Claude Code sessions on the same repo at once, without them stepping on each other, using git worktrees.
A worktree is a separate working directory with its own files and branch that shares the repository's history and remote with our main checkout. Give each session its own worktree, and one can build a feature while another fixes a bug.
Let's get started.
Starting A Worktree Session
We pass --worktree (or -w) with a name:
claude --worktree samantha-searchClaude Code creates the worktree at .claude/worktrees/samantha-search/ in our repo root, on a new branch named worktree-samantha-search, and starts the session inside it. Running the same command with a different name in another terminal gives us a second isolated session. If we leave the name off, Claude makes one up, like bright-running-fox.
The docs suggest one small bit of housekeeping so the worktrees don't show up as untracked files:
echo ".claude/worktrees/" >> .gitignoreWe can also just ask Claude to "work in a worktree" in the middle of a session.
Bringing Our Env Files Along
A worktree is a fresh checkout, so gitignored files like .env aren't there. A .worktreeinclude file in the project root fixes that. It uses .gitignore syntax:
.env
.env.local
config/secrets.jsonOnly files that match a pattern and are gitignored get copied, so tracked files are never duplicated. This applies to every worktree Claude Code creates with git, including ones for subagents.
Dependencies still need installing, so it's worth asking Claude to run our setup in the new worktree first.
Picking The Starting Point
By default, new worktrees branch from the repo's default branch on the remote, usually main. To branch from our current local HEAD instead, with any unpushed commits, we set worktree.baseRef:
{
"worktree": {
"baseRef": "head"
}
}We can also start straight from a pull request. Quoting matters here, since the shell treats # as a comment:
claude --worktree "#1234"That fetches the PR's head commit from origin and creates the worktree at .claude/worktrees/pr-1234. Neat!
Cleaning Up
When we exit, Claude checks the worktree for work we'd lose. If it's clean, an unnamed session's worktree and branch get removed automatically. If there are changes or new commits, Claude asks whether to keep or remove it. Keeping it prints a claude --worktree <name> --resume command for coming back later.
Runs with -p have no exit prompt, so their worktrees stick around. Plain git handles those:
git worktree list
git worktree unlock .claude/worktrees/hal-refactor
git worktree remove .claude/worktrees/hal-refactorThe unlock is only needed if git refuses because the worktree is locked.
Subagents In Worktrees
Subagents can get their own worktrees too. We can ask Claude to "use worktrees for your agents", or make it permanent with isolation: worktree in a custom subagent's frontmatter:
---
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.A subagent's worktree is removed automatically when it finishes without changes.
One last thing worth knowing: worktrees share more than history. Choosing "Yes, and don't ask again" for a Bash command in a worktree saves the rule to the main checkout's .claude/settings.local.json, so it applies across every worktree of the repo. Project-scoped plugins load in worktrees too.
More info about worktrees can be found at the Claude Code docs.