الانتقال إلى المحتوى
Claude وأدوات الـ AI للبرمجةClaude Code· 6 دقائق للقراءة

Parallel Claude Code Sessions Need Worktrees, Not Just More Terminals

Separate conversations still share one checkout's files, branch and index. A worktree per session keeps their edits apart.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Claude Code 2.x
محتويات المقال

You open two terminals in the same repo. In the first, Claude adds rate limiting to the API middleware. In the second, Claude fixes a flaky date test. Twenty minutes in, the test session runs the whole suite, sees three middleware tests failing because the first session is halfway through its change, and "fixes" them by putting the old middleware code back. Then the first session commits with 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 Claude Code session is independent, so I can run several in the same checkout and their edits won't collide." There's a lot of truth in the first half:

  • 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.

The half-truth: the conversations are isolated, the working directory isn't. Both sessions act on one set of files, one branch and one staging area, and each makes decisions from a state the other is changing.

What actually happens

One checkout is one shared state

A checkout is more than files. It has one 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 stash or git reset in one session changes the files under the other. git add -A in 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

A is, in the docs' words, "a separate working directory with its own files and branch, sharing the same repository history and remote as your main checkout". Git's own docs add that a linked worktree shares everything "except per-worktree files such as HEAD, index, etc.", so each one gets its own branch and staging area.

Claude Code builds this in. 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.

Bash
# Terminal 1cd ~/src/shop && claude# Terminal 2: same files, same branch, same indexcd ~/src/shop && claude

With worktrees, the story plays out differently. The test session runs the suite against its own copy, where the middleware is untouched, so the only failure it sees is its own. Each session commits to its own branch, and you decide how the two branches come together.

Two sessions, one checkoutOne worktree per session
Files on diskShared: each sees the other's half-done editsA separate copy per session
Branch and staging areaOne HEAD, one indexIts own branch and index
Tests and buildsRun against both tasks at onceRun against this task only
/rewindDoesn't capture the other session's editsEach session's edits stay in its own files
History and remoteSharedShared, through the same .git

What a worktree doesn't give you

A worktree is a fresh checkout. Gitignored files such as .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:

text .worktreeinclude
.env.env.local

Keep it to small files, since it copies rather than links. For a large directory like node_modules, the worktree.symlinkDirectories setting links it instead, or put the install command in CLAUDE.md.

The isolation also has edges. The docs scope worktrees to isolating "file edits", so anything outside the files is still shared: two dev servers on the same port or two test runs against one local database still collide. And the isolation checks read paths and command text, so a command that writes into the main checkout without running git there, such as cp or a shell redirect, isn't refused by them.

How to apply it in your project

  1. Prepare the repo onceAdd .claude/worktrees/ to .gitignore, and a .worktreeinclude for the gitignored files sessions need.
  2. Name each taskStart every parallel task with claude --worktree <task-name>. The name becomes the branch, so make it describe the work.
  3. Split what worktrees don'tIf sessions run servers or touch a database, give each its own port or database through its environment.
  4. Keep one session per checkoutIf you open a second session for a quick question, keep it read-only, or start it in a worktree too.
  5. Merge through branchesReview 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 .env with .worktreeinclude and install dependencies.
  • Worktrees isolate files, not ports or databases, and -p runs leave their worktrees for you to remove.

📚 المصادر

  1. Run parallel sessions with worktrees — Claude Code Docs
  2. Common workflows — Claude Code Docs
  3. Best practices for Claude Code — Claude Code Docs
  4. Checkpointing — Claude Code Docs
  5. git-worktree Documentation — Git

التعليقات (0)