Back to all notes
Agents
1 week ago

Git worktrees for parallel agent sessions

Run each agent session in its own git worktree so parallel edits never collide. Claude Code has native support: claude --worktree, or isolation: worktree on a subagent.

The problem

Two agents working in the same checkout will trip over each other — one's half-finished edit is the other's broken file. The fix is not coordination, it is isolation.

What a worktree is

A git worktree is a second working directory backed by the same repository: its own files and branch, sharing history and remotes with your main checkout. Edits in one never touch the other.

git worktree add ../proj-feature-a -b feature-a
git worktree list
git worktree remove ../proj-feature-a

Claude Code does it for you

You rarely need the raw commands. Start a session in a fresh worktree with claude --worktree feature-auth (or -w); it creates one under .claude/worktrees/ on a new branch. Mid-session, asking Claude to "work in a worktree" triggers the same thing through its EnterWorktree tool.

For parallel subagents

The real payoff is parallel agents that edit code without conflicting. Ask Claude to "use worktrees for your agents", or set it permanently on a custom subagent with isolation: worktree in its frontmatter. Each agent gets its own directory, so their edits cannot collide by construction.

One gotcha

Worktrees do not carry your gitignored files — a fresh worktree has no .env. Claude Code's answer is a .worktreeinclude file (gitignore syntax) listing what to copy across. Wire it up before an agent in a clean worktree fails on a missing local config and you burn ten minutes wondering why.

#git#worktrees#agents#claude-code
Found this useful?

Related notes