Claude Code with git worktrees: the practical setup

One agent in your working directory is fine. Two is a correctness problem. Claude Code now ships the fix as a flag, and enforces it at the tool layer rather than asking the model to behave.

By the Continuum team. We build a workbench that runs Claude Code, Codex, and their peers, so the model rates quoted here are the ones our own cost analytics ship with.

The short version

Run claude --worktree <name> (or -w) to create an isolated worktree and start Claude in it. As of August 2026 the worktree lands under .claude/worktrees/<name>/ at the repository root, on a new branch called worktree-<name>, branched from your default branch. Add .claude/worktrees/ to .gitignore, and list any gitignored config in a .worktreeinclude file so it is copied in. While the session is in a worktree, Claude Code blocks edits, command working directories, and git redirects that would reach the main checkout. On exit it offers to remove the worktree, and keeps it if there is work in it.

What you need to know
  • claude --worktree feature-auth is the whole setup. -w is the short form.
  • Default location is .claude/worktrees/<name>/, branch worktree-<name>.
  • Put .claude/worktrees/ in .gitignore before you start.
  • .worktreeinclude carries .env and friends into every new worktree.
  • Isolation is enforced at the tool layer, not left to the model.
  • Exiting an interactive session offers cleanup. -p runs clean up nothing.
  • Subagents get their own worktrees with isolation: worktree in frontmatter.

The one-flag version

From inside the repository.
# one isolated session
claude --worktree feature-auth

# short form, second terminal, second task, no collision
claude -w search-index

# no name: Claude generates one, such as bright-running-fox
claude -w

That creates a worktree at .claude/worktrees/feature-auth/ in the repository root, on a new branch named worktree-feature-auth, and starts the session inside it. Run it again in another terminal with a different name and you have two sessions that cannot touch each other's files.

What the flag does that you would otherwise do by hand.

StepBy handWith --worktree
Create the worktreegit worktree add -b …Automatic
Pick a branch nameYour conventionworktree-<name>
Pick a baseWhatever HEAD isDefault branch, or HEAD if configured
Copy .envA cp in your script.worktreeinclude
Keep the agent out of mainHopeEnforced per tool call
Clean up afterwardsgit worktree removePrompted on exit

What the flag actually creates

After two worktree sessions.
app/                              <- main checkout, your branch
  .git/
  .gitignore                      <- contains .claude/worktrees/
  .worktreeinclude                <- committed; lists .env and friends
  .claude/
    worktrees/
      feature-auth/               <- branch worktree-feature-auth
      search-index/               <- branch worktree-search-index
  src/

Which commit it branches from

By default a new worktree branches from the repository's default branch on the remote, not from wherever you happen to be standing. The setting is worktree.baseRef, and it takes two values.

worktree.baseRefBranches fromUse when
"fresh" (default)The remote default branch, usually mainYou want a clean tree matching the remote
"head"Your current local HEADThe agent needs your unpushed, in-progress work
.claude/settings.json
{
  "worktree": {
    "baseRef": "head"
  }
}

Branching from a pull request

Quote it, or your shell eats the # as a comment.
claude --worktree "#1234"

Claude Code fetches pull/1234/head from origin and puts the worktree at .claude/worktrees/pr-1234. A full GitHub pull request URL works too. This is the fastest route to "review this PR without touching what I am doing", which was the original human use for worktrees long before agents existed.

Reusing a name

Passing a name whose directory already exists opens that worktree rather than creating a new one. With the default "fresh" base it also resets to the default branch, but only when the worktree is clean, still on the branch Claude Code made for it, and either has no commits of its own or has had its pull request merged and its remote branch deleted. Anything else, including any reuse under "head", reopens at the old tip. The conservative direction is the correct one: a reset that discards commits should require every condition, not most of them.

Carrying the gitignored files across

A worktree is a fresh checkout, so .env, node_modules, and build output are not there. The config half is solved by a committed file; the dependency half is a normal install.

.worktreeinclude, in the project root. Uses .gitignore syntax.
.env
.env.local
config/secrets.json

Only files that match a pattern and are gitignored are copied, so tracked files are never duplicated. It applies to every worktree Claude Code creates with git: --worktree sessions, subagent worktrees, and desktop parallel sessions alike.

For dependencies, run the install in the worktree, or ask Claude to. If you run several worktrees regularly, move the project to a package manager with a content-addressed store so the second install costs almost nothing.

cd .claude/worktrees/feature-auth
pnpm install --prefer-offline

The isolation is enforced, not requested

This is the part that makes the built-in flag different from a shell script that runs git worktree add and then claude. While a session is isolated in a worktree, Claude Code blocks tool calls that would reach the main checkout. The same checks cover every subagent that session spawns, interactive or background.

Three checks, applied per tool call.

CheckBlocks
File editsAn Edit, Write, or NotebookEdit targeting a path in the main checkout
Command working directoryA Bash or PowerShell command whose cwd resolves to the main checkout, or that cannot be verified as staying outside it
Git redirectsA command redirecting git into the main checkout via git -C, --git-dir, GIT_DIR, GIT_WORK_TREE, or a cd before the git call

What a worktree still shares with the main checkout

SharedConsequence
The repository .git directorygit commit works from inside a worktree even with the sandbox on
Project-scope pluginsInstalled once in the main checkout, loaded in every worktree
Saved permission approvals"Yes, do not ask again" writes to the main checkout's .claude/settings.local.json
History, branches, remotes, tagsA commit made in one worktree is immediately visible in all

The approvals row is the useful one in practice: an approval granted inside a throwaway worktree now applies everywhere and survives that worktree being removed, so you are not re-approving the same test command in every session.

Three ways a session could reach the main checkout from its worktree, all refused per tool call, alongside the one channel that stays open because both share the same git directory three ways out, checked on every tool call isolation boundary the worktree .claude/worktrees/ feature-auth the session and its subagents main checkout your branch your uncommitted work one shared .git Edit or Write into ../app cd ../app && npm test git -C ../app commit git commit, via the shared .git A shell script that only changes directory never catches the third one.

Cleanup, and what happens if you skip it

When you exit an interactive worktree session, Claude Code inspects the worktree for anything removal would destroy: changed files, untracked files, and new commits.

State on exitWhat happens
Clean, unnamed sessionWorktree and branch removed automatically
Clean, named sessionYou are prompted, so you can keep it for later
Has changes or commitsYou are prompted to keep or remove
Non-interactive (-p)Nothing. There is no exit prompt
Cleaning up what -p left behind.
git worktree list
git worktree remove .claude/worktrees/feature-auth

# if it has uncommitted work you have decided to discard
git worktree remove --force .claude/worktrees/feature-auth

# after deleting a directory by hand
git worktree prune -n -v && git worktree prune

Subagents get their own worktrees too

A single session can fan out. Ask Claude to "use worktrees for your agents", or make it permanent for a custom subagent with one line of frontmatter.

.claude/agents/refactorer.md
---
name: refactorer
description: Applies mechanical refactors across many files
isolation: worktree
---

Apply the requested refactor across every affected file, then run the tests
and report the results.

Each such subagent gets a temporary worktree, using the same base branch rule as --worktree. Claude Code removes it automatically when the subagent finishes with no changes; one that produced changes stays on disk.

When to create the worktree with git instead

The flag covers new work from a clean base. Two cases still want git directly: checking out a branch that already exists, and putting the worktree somewhere other than .claude/worktrees/.

# an existing branch, as a sibling of the repository
git worktree add ../app-bugfix fix-issue-456
cd ../app-bugfix && claude

# a new branch from a specific base
git worktree add -b feature/x ../app-x origin/release-2
cd ../app-x && claude

# read-only look at a tag, no branch involved
git worktree add --detach ../app-v1 v1.0.0

Which route for which job.

You wantUse
A fresh branch for a new taskclaude -w <name>
To work on a pull requestclaude -w "#1234"
An existing branchgit worktree add, then claude
A path outside the repositorygit worktree add, then claude
A non-git VCSA WorktreeCreate hook
A tmux pane per sessionclaude -w <name> --tmux

Getting the work back, and where this stops scaling

01

Review the branch diff, not the conversation

cd .claude/worktrees/feature-auth
git diff main...HEAD

One worktree means one clean diff, with nothing from another task mixed into it. Read the diff. The transcript tells you what the agent believes it did.

02

Open a pull request as usual

git push -u origin worktree-feature-auth
gh pr create --fill

The point of a branch per session is that the result rejoins your normal process instead of needing a special one.

03

Let the exit prompt clean up

Exit the session and accept the removal. If you left it running with -p, remove it yourself with git worktree remove and delete the branch once it is merged.

  1. Attention. Past three or four sessions, terminal tabs stop being a usable index of what is running and which one is waiting on you.
  2. Quota. Consumption scales with sessions, not with your patience. Four agents drain a plan roughly four times as fast, and the failure mode is discovering it mid-task.
  3. Review. The ceiling that actually binds. Four agents produce more diff than one person reads carefully, and unreviewed agent output is a liability rather than an asset.

Questions people ask

Run claude --worktree <name>, or -w for short, from inside the repository. Claude Code creates a worktree at .claude/worktrees/<name>/ on a new branch called worktree-<name> and starts the session there. Run it again with a different name in another terminal for a second isolated session.

Under .claude/worktrees/<name>/ at the repository root by default. Add .claude/worktrees/ to your .gitignore so they do not appear as untracked files. A WorktreeCreate hook can place them somewhere else entirely.

You can start them, but they will overwrite each other with no error and no warning, and you will not be able to reconstruct which agent did what. Give each one its own worktree.

Add a .worktreeinclude file to the project root listing the paths, using gitignore syntax. Only files that both match a pattern and are gitignored are copied. It is not processed when a WorktreeCreate hook replaces the default logic.

The repository default branch on the remote, which is the "fresh" value of the worktree.baseRef setting. Set it to "head" to branch from your current local HEAD instead, which is what you want when the agent needs your unpushed work.

On exit from an interactive session, yes, with a prompt. A clean unnamed session is removed automatically; anything with changes or commits asks first. Non-interactive runs with -p have no exit prompt and clean up nothing, so remove those with git worktree remove.

Yes. Add isolation: worktree to a custom subagent frontmatter, or ask Claude to use worktrees for its agents. A periodic sweep removes the leftovers, skipping any that still hold changes or unpushed commits.

Three or four before terminal tabs stop being a usable index and before review becomes the bottleneck. Quota is the other constraint: parallel sessions consume the allowance in parallel too.

Sources

Every figure above was read from these pages on August 2026. Vendors reprice without notice; if you find a stale number, tell us.

  1. Claude Code: run parallel sessions with worktrees
  2. Claude Code CLI reference
  3. git-worktree manual
  4. Claude Code documentation
Try it

Four agents.
One sidebar.

Continuum creates the worktree, runs the agent, and shows every session with its own diff and status.

free app · your subscriptions · local-first