Intermediate 1 concept to know first

Worktrees

A separate git checkout for each Claude Code session or subagent, with its own files and branch, so parallel work never edits the same files.

The animation needs JavaScript; every step is described below.

0:15

Know these first

1 concept · basics first
  1. Context

    The context window is everything Claude can see in a session: your messages, the files it reads, tool output and auto-loaded instructions. It fills as you work, and Claude Code compacts it when it gets full.

  2. Worktrees · you are here

Run two Claude Code sessions in the same folder and they share one set of files: while one builds a feature, the other’s bug fix lands in the same working tree, and the edits tangle. A git worktree fixes that. It’s a second working directory with its own files and its own branch, sharing the repository’s history and remote with your main checkout. Give each session its own worktree and their edits can’t collide.

Worktrees don’t run anything in parallel themselves. They’re the isolation underneath the things that do: sessions you start yourself, subagents, and the background sessions you dispatch.

Start a session in one

claude --worktree feature-auth

--worktree (or -w) creates the worktree at .claude/worktrees/feature-auth/ on a new branch, worktree-feature-auth, and starts Claude there. Run it again with another name in a second terminal for a second isolated session; leave the name out and Claude picks one. Mid-session, you can also just ask Claude to “work in a worktree”.

By default a new worktree branches from your repository’s default branch on the remote, so it starts clean. Set worktree.baseRef to "head" in settings to branch from your current local work instead.

Subagents can have their own

A subagent can run in a temporary worktree so parallel edits don’t conflict: ask Claude to “use worktrees for your agents”, or add isolation: worktree to a custom subagent’s frontmatter. If the subagent finishes without changes, its worktree is removed for you.

Cleaning up

When you exit, Claude checks the worktree. A clean one from an unnamed session is removed along with its branch; one with changes or new commits prompts you to keep it or remove it. Kept worktrees can be resumed with claude --worktree <name> --resume.

The catch

A worktree is a fresh checkout. Your dependencies aren’t installed there, and gitignored files like .env are missing. Run your setup in it, and list files to copy in a .worktreeinclude file at the project root (it uses .gitignore syntax). Add .claude/worktrees/ to your .gitignore too.

Worktrees need git; other version control systems can supply their own logic through WorktreeCreate and WorktreeRemove hooks. And several sessions at once means several times the tokens. Worktrees isolate files on disk; to undo within a single session, that’s checkpoints.

Where it sits

Before this

Context
Worktrees

Leads to

Nothing yet.

The ideas underneath · Field Guide

Sources

  1. Run parallel sessions with worktrees . Claude Code docs: --worktree, subagent isolation, .worktreeinclude, cleanup
  2. Run agents in parallel . Claude Code docs: how worktrees fit beside subagents, agent view, agent teams and workflows

Verified against the official Claude Code docs on October 1, 2026 · next review by January 1, 2027. Claude Code changes quickly; the docs are the source of truth.