↓ Skip to main content

Video 8 min

Claude Code Worktrees: Running Parallel Agents Without Collisions

How to use Claude Code worktrees: claude -w, .worktreeinclude for .env, node_modules, base ref and cleanup, and running parallel agents in tmux.

Narrated in English Subtitles: English, 日本語, 한국어, Tiếng Việt, 简体中文

Transcript

One day, I had five Claude Code sessions working in the same checkout of my marketplace app. A one-line fix to a test config file survived four failed builds. Then, ready to commit, it was gone. Git said the working tree was clean. The file held another session's version.

Git keeps no record of a lost update like that. No reflog entry. No stash. Just an ordinary modification time.

That's how I came to worktrees. The hard way. I run a lot of agents in parallel. Start each Claude Code session with claude dash w and a name, and it can only edit files in its own folder, on its own branch. The catch is that a worktree is a fresh checkout.

Your dot env, your node modules, anything git ignores, isn't there until you copy or install it. Nearly every problem I've had came from that.

That same day, a deploy script that builds from the working tree came close to shipping another session's unfinished work to production. Later, a pre-commit hook stashed everything unstaged while one session committed. Another session's edits vanished from disk, and the commit aborted. That's the case for worktrees in one line: a working tree with several writers is a race, and the loser can't see it happen. Give each agent its own working tree, and the race goes away.

I tested claude dash w in a throwaway repo, alongside the docs. Each worktree gets a folder under dot claude, and a branch cut from the remote's default branch. Or from your local HEAD, but never from a branch you name. Inside a worktree, Claude Code refuses edits aimed at the main checkout. But the docs say plainly what it doesn't catch: a cp or a shell redirect.

Worktrees share one dot git. And a "Yes, and don't ask again" given in one applies to all of them. Exit an interactive session, and a clean, unnamed worktree is removed. A named one asks first. But claude dash p has no exit prompt, so nothing is cleaned up.

Its lock stays behind, held by a process that no longer exists. Git refuses to remove it until a later sweep releases it, or you unlock it. Harmless once you know. Slightly alarming the first time.

A worktree has tracked files and nothing else. Gitignored ones come along only if they're listed in a dot worktreeinclude file. It uses gitignore syntax, and copies only files that are gitignored. Worktrees made with git worktree add don't get it. I'd put more than dot env in it.

My deploy script was changed to build from a clean worktree, so uncommitted edits could never ship. But the clean tree lacked the gitignored signing certificates one feature needs. The image built anyway, and that feature failed in production for thirteen days. The files you forget rarely fail loudly. So before a worktree ships anything, ask which ignored files the build reads.

Dot worktreeinclude won't help with node modules. Every worktree that runs your code needs its own. In a demo repo, a new worktree takes eighty-four kilobytes. After an install, it's the full forty-five megabytes again. My mobile app's repository has nineteen worktrees.

The biggest I measured is twelve gigabytes, and the four biggest have their own node modules. The options, simplest first. Install in every worktree: slow, but never surprising. Or clone node modules from a checkout with the same lockfile. On macOS, the demo took zero point eight seconds.

Native build folders can bite: my iOS Pods were built for an older SDK, and had to be installed fresh. Or use pnpm. With its single store, a second install of the demo took about five seconds. Or install in a WorktreeCreate hook. I haven't tried that, and the hook would have to copy your dot env too.

I live in tmux, so it's one pane per worktree. Here, three agents start at once, each told to change the same file. Two of them edited it at the same time. Neither noticed, because each copy was a different file on disk. The main checkout stayed clean.

That leaves three branches to review. Same lines still conflict. It just arrives in a merge, where you can see it, instead of as a silent overwrite.

Worktrees age. Not long ago, an over-the-air update went out from a worktree prepared two or three days earlier. It quietly reverted two fixes that had shipped from elsewhere in the meantime. It was caught and replaced the same day. The worktree was perfectly isolated, and that was the problem: it never saw those fixes.

A git one-liner would have caught it before release, by checking whether main is already in the worktree. Nothing keeps old worktrees current. So check before you ship from one. You don't even need a terminal per agent. A subagent with isolation set to worktree always runs in a temporary worktree of its own.

If it makes no changes, its worktree is removed. And it takes its instructions from the main conversation, not the worktree's CLAUDE.md file.

Here's the confession. In my app's repository, the Claude Code worktrees folder is empty. All nineteen worktrees were made with plain git worktree add. And mostly, it isn't me making them. In eighty-five of my Claude Code transcripts on this laptop, an agent runs git worktree add.

Plain git wins when I need a specific branch, like a release branch kept for hotfixes. Or a worktree the sweep will never remove. Or to ship exactly one fix: a detached worktree at the commit in production, with my fix cherry-picked in, and nothing else. Claude dash w wins when I start a session by hand and want the branch, the dot env, the fence and the cleanup handled. And the two mix fine.

So it comes down to this. One worktree per session. Every gitignored file the build needs, listed. A plan for node modules. And a check before shipping from an old worktree.

Worktrees stop agents overwriting each other's files; they don't stop them disagreeing, so you'll still be reviewing three branches at the end. But a disagreement in a merge is one I can see. Not a fix that quietly vanished. The further reading is linked in the description. The full post, with the commands and config to copy, is on jared.lynskey.co.nz.

More videos

Cloudflare 520, nginx 499 and Postgres Stalls: Keeping Webhooks Alive

pytest-testmon: Run Only the Tests Affected by Your Code Change

Replacing My Self-Hosted Expo Update Server with Codemagic Patch

How to Debug an iOS App in Xcode: Breakpoints, LLDB, Instruments and the Simulator