Claude Code

Parallel workspaces with worktrees

Worktree startup, base branches, PR worktrees, local file copying, subagent isolation, cleanup, and non-Git hooks.

A Git worktree is another working directory sharing repository history and remotes. Claude Code uses it to isolate parallel sessions so feature and bug-fix edits do not collide in one checkout.

Worktrees isolate file changes. See Subagents for delegation and Sessions for naming/resumption.

#When to use one

ScenarioRecommendation
Two sessions editing one repositoryGive each a worktree
Start clean from remote default branchDefault --worktree name
Subagent may editisolation: worktree
Local unpushed foundation commitsworktree.baseRef: head
Need ignored local configuration.worktreeinclude

Monorepos can reduce checkout size with sparsePaths and symlinkDirectories; see Large codebases. Desktop parallel sessions can create worktrees automatically; this guide focuses on CLI.

#Start a worktree session

claude --worktree feature-auth
ItemDefault
Directory.claude/worktrees/<name>/ under repository root
Branchworktree-<name>

Start another isolated task:

claude --worktree bugfix-123

Or generate a name:

claude --worktree

You can also ask Claude to work in a worktree. EnterWorktree creates and enters it; switching to another managed worktree leaves the original on disk.

Before first interactive worktree use, run Claude in the repository and accept workspace trust. Otherwise the command asks you to trust it first. Noninteractive print-mode worktrees skip that interactive trust check.

Ignore generated directories:

.claude/worktrees/

#Base branch

By default, new worktrees start from origin/HEAD, falling back to local HEAD if no remote is available or fetch fails.

To include the current branch's unpushed commits:

.claude/settings.json
{
  "worktree": {
    "baseRef": "head"
  }
}
ValueMeaning
freshPrefer origin/HEAD; default
headCurrent local HEAD

For a GitHub PR, use a #number or full PR URL:

claude --worktree "#1234"

Claude fetches pull/<number>/head from origin and creates .claude/worktrees/pr-<number>.

#Copy local configuration

A new checkout does not automatically include untracked .env files or local secrets. A root .worktreeinclude uses gitignore-style patterns to copy selected ignored files:

.worktreeinclude
.env
.env.local
config/secrets.json
BoundaryDetail
Ignored files onlyTracked files are not copied through this mechanism
Multiple entry pointsCLI, subagent worktrees, Desktop parallel sessions
Custom WorktreeCreateMust implement copying yourself

Never commit real keys. Prefer user settings or shell variables for Passion8 credentials; copy project env files only when the task needs them.

#Subagent isolation

.claude/agents/fixer.md
---
name: fixer
description: Fix an independent issue in an isolated worktree.
tools: Read, Grep, Glob, Edit, Bash
isolation: worktree
---

Keep the change focused. Report changed files and validation results when finished.

You can request worktrees for agents directly. Their base strategy is the same: remote default unless baseRef is head. Unchanged temporary worktrees are removed; changed ones retain information for review.

#Cleanup

State on exitBehavior
No changes, untracked files, or new commitsRemove workspace/branch; named sessions may prompt to keep
Any changes or new commitsAsk to keep or delete; deletion discards them
Print modeNo automatic exit cleanup; remove manually

Periodic cleanup of subagent/background worktrees requires:

  • Older than cleanupPeriodDays.
  • No uncommitted changes.
  • No untracked files.
  • No unpushed commits.

Explicit --worktree workspaces are not removed by that background sweep. Active agent worktrees are locked against concurrent cleanup and unlocked after completion.

#Manual management

For explicit location/branch control:

git worktree add ../project-feature-a -b feature-a
git worktree add ../project-bugfix bugfix-123

Start Claude inside it:

cd ../project-feature-a
claude
CommandPurpose
git worktree listList workspaces
git worktree remove ../project-feature-aRemove a clean workspace
git worktree remove --force ../project-feature-aDiscard a workspace containing uncommitted changes

Each directory needs the project's applicable dependencies, virtual environments, generated files, and initialization steps.

#Other version-control systems

For SVN, Perforce, Mercurial, or custom isolation, implement WorktreeCreate/WorktreeRemove hooks:

.claude/settings.json
{
  "hooks": {
    "WorktreeCreate": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "bash -c 'NAME=$(jq -r .name); DIR=\"$HOME/.claude/worktrees/$NAME\"; svn checkout https://svn.example.com/repo/trunk \"$DIR\" >&2 && echo \"$DIR\"'"
          }
        ]
      }
    ]
  }
}

The hook reads the requested name from stdin, creates a directory, and prints its path. It replaces Git creation and must handle local configuration copying itself.

#Common mistakes

MistakeResultCorrection
Expect env files automaticallyMissing local configurationUser settings, shell env, or worktreeinclude
Expect local unpushed commits under fresh modeStarts from remote baseUse baseRef head
Commit .claude/worktreesUnwanted nested checkoutsAdd ignore entry
Expect print-mode cleanupWorkspace remainsRemove manually
Agents edit one checkoutConflicts and stale contextIsolate writers

#Official references

Support

Need help?

For setup, billing, or model issues, email us. Check the status page for uptime.

WeChat / QQ support is available at the bottom right.