
Parallel Claude Code Sessions Need Worktrees, Not Just More Terminals
- كاتب موثّق
- Claude Code 2.x
محتويات المقال
git add -A, and the date-test fix ships inside a commit titled "Add rate limiting". Nothing crashed. Each session did something reasonable with the files it saw.
The common misconception
Each session has its own conversation and context window; one never sees the other's chat. Claude reads files fresh on each tool call, so a session picks up the other one's edits the next time it reads. Claude's Edit tool matches against the file's current content, so two sessions rarely garble the same line. When the tasks touch different files, it often works fine for a while.
What actually happens
One checkout is one shared state
HEAD, so one branch checked out at a time, and one index, the staging area your next commit comes from. Two sessions in it share all of that:
Half-finished work is visible. Tests, builds and linters run against both sessions' edits at once, so a failure can belong to the other task, and a session that "fixes" it may undo the other's work in progress. Git operations are global. git checkout,git stashorgit resetin one session changes the files under the other.git add -Ain one sweeps the other's edits into its commit.Undo doesn't know about the other session. track files edited in the current session, and "edits from other concurrent sessions are normally not captured, unless they happen to modify the same files as the current session".
What a worktree gives each session
claude --worktree feature-auth creates .claude/worktrees/feature-auth/ on a new branch named worktree-feature-auth, branched from your default branch, and starts the session there. While a session is isolated, Claude Code blocks its edits to paths in the main checkout, commands whose working directory resolves there, and git redirected into it with git -C or GIT_DIR.
# Terminal 1cd ~/src/shop && claude# Terminal 2: same files, same branch, same indexcd ~/src/shop && claude# Terminal 1: .claude/worktrees/rate-limit, branch worktree-rate-limitcd ~/src/shop && claude --worktree rate-limit# Terminal 2: its own copy of the files on its own branchcd ~/src/shop && claude --worktree flaky-date-testHEAD, one index | ||
/rewind | ||
.git |
What a worktree doesn't give you
.env aren't there and dependencies aren't installed. List the small gitignored files every session needs in a .worktreeinclude at the repo root, using .gitignore syntax, and Claude Code copies them into each worktree it creates:
.env.env.localnode_modules, the worktree.symlinkDirectories setting links it instead, or put the install command in CLAUDE.md.
cp or a shell redirect, isn't refused by them.
How to apply it in your project
Prepare the repo once Add .claude/worktrees/to.gitignore, and a.worktreeincludefor the gitignored files sessions need.Name each task Start every parallel task with claude --worktree <task-name>. The name becomes the branch, so make it describe the work.Split what worktrees don't If sessions run servers or touch a database, give each its own port or database through its environment. Keep one session per checkout If you open a second session for a quick question, keep it read-only, or start it in a worktree too. Merge through branches Review each worktree-<name>branch, then merge it or open a PR, and let Claude Code clean up on exit.
Key takeaways
Parallel sessions have separate conversations but, in one checkout, share files, branch, index and test state. claude --worktree <name>gives a session its own files and branch under.claude/worktrees/.Claude Code blocks an isolated session's edits and git commands aimed at the main checkout. A worktree is a fresh checkout: bring .envwith.worktreeincludeand install dependencies.Worktrees isolate files, not ports or databases, and -pruns leave their worktrees for you to remove.
التعليقات (0)