Claude Code guide in three levels, Beginner to Advanced: hands-on lessons with checked exercises, every volatile fact cited to its source. Independent community guide.
Parallel sessions with worktrees
Advanced · Lesson 1 of 7 · about 20 minutes · Needs: Advanced index · Verified against Claude Code v2.1.308 (stable) on 2026-10-25
Goal
By the end of this lesson you can run two Claude Code sessions at once, each in its own git worktree, and merge their work into one branch.
You need
- One of your own repositories with a test command and no uncommitted changes, checked out on its default branch and up to date with a remote you can push to (Watch out says why), and your copy of the practice template with Node.js LTS and the Advanced checks (Practice): the check runs from the copy. Set up the repository's
.practice/folder as the Intermediate index describes, if you haven't yet. - If you haven't run
claudein that repository before, run it there once and accept the trust dialog: until thenclaude --worktreeexits with an error (Start Claude in a worktree). - macOS, Linux or WSL 2, and three terminals open at the repository's root: two for the sessions, one for git.
- Usage: moderate. Two sessions work at the same time.
The idea
A git worktree is a second working folder, on its own branch, that shares the repository's history. claude --worktree <name> (or -w <name>) creates one under .claude/worktrees/<name>/ on a new branch, worktree-<name>, and starts the session in it (Start Claude in a worktree). Each session edits its own copy of the files. Claude Code also stops a worktree session's file-editing tools from changing your main checkout, and its commands from running there or pointing git at it (How Claude Code enforces isolation).
Commits and branches are shared. Uncommitted edits, installed dependencies and gitignored files such as .env are not: each worktree is a fresh checkout (Set up the worktree environment).
The work comes back through git: each session commits on its branch, and you bring both branches into yours. Your review sets the pace. Claude Academy puts the ceiling at "how many streams one person can review properly" (Parallel sessions and subagents), so run only as many sessions as you can read the diffs of.
Worked example
In your main checkout, ignore the folder the worktrees live in, then commit and push the rule. Without it, worktree contents show up as untracked files in your main checkout. The push puts it on the remote's default branch, where new worktrees start (Watch out says more):
printf '\n.claude/worktrees/\n' >> .gitignore git add .gitignore && git commit -m "Ignore Claude Code worktrees" git pushAfterwards
git status --shortprints nothing, andgit log -1 --onelineshows the commit.In terminal 1, start a session in a new worktree and give it a docs task:
claude --worktree docs-passTidy the wording of the README's first section, then wait for me before you commit.Claude Code creates
.claude/worktrees/docs-pass/on the branchworktree-docs-pass, and the session works there. In auto mode, the mode a session starts in, a commit, or even a push to the repository you're working in, wouldn't wait for you (Which mode a session starts in, What the classifier blocks by default). Asking it to wait is what holds the commit until step 4: auto mode treats a boundary you state in the conversation as a reason to block the matching action. It's a soft stop, which a compacted conversation can lose; for a hard one, add a deny rule (Boundaries you state in conversation).In terminal 2, start a second session with the short flag and give it a test task:
claude -w test-passThis worktree is a fresh checkout: install the dependencies, then add one missing test for a small exported function and run the tests. Wait for me before you commit.Claude Code creates
.claude/worktrees/test-pass/on the branchworktree-test-pass; the session installs the dependencies there before it adds the test.In terminal 3, in your main checkout, look at all three checkouts while both sessions wait for you:
git worktree list git status --short git -C .claude/worktrees/docs-pass status --short git -C .claude/worktrees/test-pass status --shortgit worktree listshows your main checkout first, then.claude/worktrees/docs-passon[worktree-docs-pass]and.claude/worktrees/test-passon[worktree-test-pass]; git may mark each onelockedwhile its session runs.git status --shortdoesn't list either session's edits: they happen in other folders. Eachgit -Ccommand lists only the changes of the session in that folder, such as the README indocs-pass, and never the other session's. Now tell each session to commit:git log --oneline --graph --allthen shows a new commit on each branch.Still in your main checkout, merge both branches, then run your tests:
git merge worktree-docs-pass git merge worktree-test-passgit mergebrings each branch's commits into yours. The first merge just moves your branch forward, and the second makes a merge commit and opens your editor for its message: save and close it. Run this step yourself: while a session is isolated in its worktree, Claude Code blocks its git commands that reach into the main checkout. If both tasks changed the same lines, git stops with a conflict: resolve it, or startclaudein the main checkout and ask it to.In each session, run
/exit. The worktree's branch has a new commit, so Claude Code asks whether to keep or remove the worktree: choose remove (Clean up worktrees).git worktree listno longer shows either worktree.
[!CAUTION] Removing a worktree deletes its folder and its branch, along with any work in them you haven't merged. Merge first, or choose keep.
Your turn
Land two backlog changes made at once, with neither session touching the other's files. Pick two real changes from your repository's backlog that don't change the same lines and don't build on each other, such as a small bug fix in one module and a missing test for another. Git merges changes to different parts of one file cleanly, but two changes to the same lines collide when you bring them together (How conflicts are presented), and each session starts without the other's change. Keep it to two, because each session's diff still needs your review.
No backlog to pick from? Your copy of the practice template has two such changes:
findLinksinsrc/links.jslets a~~~line close a code block that opened with three backticks, so it reports links inside the block.- No test covers
src/cli.js, the command that prints each broken link and sets the exit code.
Before you start the sessions, decide what a fresh worktree of your repository lacks before its tests can run. Dependencies are the usual gap: tell each session to install them, as the Worked example's test session did. A gitignored file the tests read, such as a local config, is the other: list it in a .worktreeinclude file at your repository root, which copies only the files that match a pattern and are gitignored, or have the session create a test-only copy (Copy gitignored files into worktrees). Every worktree gets its own copy of each file you list, so list only what the tests need.
If the work they should build on isn't on the remote's default branch yet, get it there first, or add "worktree": {"baseRef": "head"} to .claude/settings.local.json, your personal settings file from Intermediate lesson 4, so new worktrees start from your local HEAD (worktree.baseRef).
Acceptance criteria:
A committed
.gitignoreignores.claude/worktrees/, and doesn't ignore the rest of.claude/.Each change is made in its own session, started with
claude --worktree <name>, and committed on that worktree's branch..practice/a-1-worktrees.txtholds the output ofgit worktree list --porcelain, saved from the main checkout while both worktrees existed and both had committed:git worktree list --porcelain > .practice/a-1-worktrees.txtBoth changes are in your branch, by merge, rebase or squash, whichever your team uses.
Your tests pass with both changes in (not checked).
Both worktrees are removed, and any branch you kept is deleted only once its change is in your branch.
Check
You are done when all of these are true:
- A committed
.gitignoreignores.claude/worktrees/, and not the rest of.claude/. -
.practice/a-1-worktrees.txtlists two worktrees under.claude/worktrees/whose commits split from each other, and both changes are in your branch. - Neither of those worktrees still exists:
git worktree listdoesn't show them. - Self-check (not tested): you can name what a fresh worktree of your repository lacked, such as its dependencies or a gitignored config file, and say whether you listed it in
.worktreeincludeor had the session set it up, and why.
Run npm run check -- a-1 --dir <your repo> from your practice copy.
If the first item fails, run git check-ignore -v .claude/worktrees/x in your repository: it should name your committed .gitignore (git-check-ignore). If the second item fails, read the hint the check prints: save the file after both sessions commit and before you remove either worktree. If the third item fails, exit any Claude Code session still open in a worktree it names: /exit, then remove it, or keep it and run git worktree remove <path> from your main checkout. Run git worktree unlock <path> first only when git says the worktree is locked and no session is open in it (Clean up worktrees).
Watch out
- In most repositories on macOS, Linux and WSL 2, if a worktree session asks about a Bash command and you choose "Yes, and don't ask again", Claude Code saves the rule to your main checkout's
.claude/settings.local.json. It then applies in the main checkout and in every worktree of the repository, and it stays after the worktree is removed (What worktrees share with the main checkout). Approve only commands you'd allow everywhere. - Running several sessions at once multiplies token usage (Run agents in parallel). Give each session one small task, and exit it once its branch is merged.
- A new worktree branches from the remote's default branch, not from your local
HEAD(Choose the base branch). Commits that aren't on that branch yet, such as unpushed commits or your feature branch's work, aren't in it, and merging its branch into a branch that's behind the remote's default branch brings in the commits that branch lacks. With no remote, a new worktree starts from your localHEADinstead.
Go further
- Isolate subagents with worktrees:
isolation: worktreein a subagent's frontmatter gives each run a checkout of its own. - Message your other Claude Code sessions: let the sessions in your worktrees pass findings to each other.
- Run multiple Claude sessions: the Writer/Reviewer pattern, where a fresh session reviews what another one wrote.
Sources: Run parallel sessions with worktrees · All settings · Choose a permission mode · Run agents in parallel · Parallel sessions and subagents · git-worktree · git-merge · git-check-ignore
← Advanced index · Choose an orchestration pattern → · Topic: Subagents and parallel work · Stuck on this lesson?