A Git worktree is a separate working directory connected to the same repository history. Each worktree has its own files and branch, while the repository history and remote remain shared.
Imagine Claude Session A is building authentication while Session B is fixing a login bug. If both use the same checkout, their edits can interfere. With worktrees, each session gets an isolated checkout.
| Without Worktrees | With Worktrees |
|---|---|
| Multiple sessions share files. | Each session gets separate files. |
| Edits can interfere. | Edits stay isolated. |
| Parallel work is harder. | Feature and bugfix work can proceed together. |
Use the --worktree or -w option:
claude --worktree feature-auth
By default, Claude Code creates the worktree under .claude/worktrees/<name>/ and uses a branch named
worktree-<name>.
claude --worktree bugfix-123
claude --worktree
claude once and accept the trust dialog.Add the default worktree directory to your project's .gitignore:
.claude/worktrees/
This prevents worktree contents from appearing as untracked files in the main checkout.
A worktree is a fresh checkout, so initialize the development environment there.
Git-ignored files such as .env are not normally present in a fresh worktree.
Create .worktreeinclude in the project root:
.env
.env.local
config/secrets.json
Only files that are both matched and Git-ignored are copied; tracked files are not duplicated.
During a session you can ask Claude to “work in a worktree”. Claude Code can create one with the
EnterWorktree tool. It can also switch to another worktree under .claude/worktrees/.
When an interactive worktree session ends, Claude checks whether removing it would delete work.
| State | Behavior |
|---|---|
| Clean unnamed worktree | Claude can remove the worktree and branch automatically. |
| Named session | Claude prompts you so you can keep it. |
| Changes, untracked files, or new commits | Claude prompts you to keep or remove it. |
Non-interactive -p | No exit prompt; cleanup is not automatic. |
git worktree remove ../project-feature-a
If it is locked:
git worktree unlock ../project-feature-a
When a session is resumed, Claude Code can return it to its worktree after verifying that the worktree is still a separate checkout.
This also applies to --continue, --resume, and supported Agent SDK resumes.
Claude Code actively protects the main checkout while a session is isolated in a worktree.
Subagents can receive their own temporary worktrees so parallel edits do not conflict.
Use worktrees for your agents.
A custom subagent can request isolation with isolation: worktree:
---
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.
Temporary subagent worktrees can be removed automatically when a subagent finishes without changes. A worktree containing changes is retained so work is not lost. Claude Code also periodically cleans eligible old subagent and background-session worktrees.
| Setting | Meaning | Useful when |
|---|---|---|
fresh | Default. Starts from the repository's default branch on the remote. | Clean independent feature work. |
head | Starts from your current local HEAD and carries local commits/state. | Isolating work that depends on in-progress changes. |
{
"worktree": {
"baseRef": "head"
}
}
worktree.baseRef accepts fresh or head, not an arbitrary branch name.You can create a worktree from a specific pull request:
claude --worktree "#1234"
Claude Code fetches the PR's change from origin and creates a dedicated worktree.
If the named worktree directory already exists, Claude Code can open it instead of creating another one.
With the default fresh base, an eligible clean worktree can be reset to the repository's default branch.
If it contains meaningful work or does not meet the reset conditions, its old tip is preserved.
A WorktreeCreate hook can replace Claude Code's default Git worktree creation logic.
This is useful for custom locations or other version-control systems.
A worktree has its own files and branch, but some resources remain shared:
.git directory.git worktree add ../project-feature-a -b feature-a
git worktree add ../project-bugfix fix-issue-456
cd ../project-feature-a
claude
git worktree list
git worktree remove ../project-feature-a
Worktree isolation uses Git by default. For SVN, Perforce, Mercurial, or another VCS, configure
WorktreeCreate and WorktreeRemove hooks to implement creation and cleanup.
When a custom hook replaces Git creation, .worktreeinclude is not processed automatically.
This can happen if the directory was deleted or a custom creation hook returned an unexpected path.
Claude Code refuses relevant symlinked .claude, .claude/worktrees, or worktree paths. Remove the problematic symlink and retry.
In the documented repository-local filter-driver case, a worktree can contain LFS pointer files.
Run git lfs pull inside the worktree to retrieve actual files.
Claude checks the worktree's Git identity. If it resolves to the main checkout or its Git metadata cannot be verified safely, Claude refuses to use it.
| Approach | Main purpose |
|---|---|
| Worktrees | Separate file edits into independent Git checkouts. |
| Subagents | Delegate pieces of work inside a Claude session. |
| Agent teams | Coordinate multiple Claude sessions working together. |
| Cross-session messaging | Let separate sessions exchange findings. |
Suppose your application needs authentication and also has an urgent production bug:
# Terminal 1
claude --worktree feature-auth
# Terminal 2
claude --worktree bugfix-login
.claude/worktrees/ in .gitignore..env automatically appears.worktree.baseRef as an arbitrary branch selector.claude --worktree <name> for parallel sessions..claude/worktrees/ in Git..worktreeinclude carefully for Git-ignored local files.head when isolated work needs current local state.claude --worktree feature-auth do?.claude/worktrees/ to .gitignore?.worktreeinclude?fresh and head?claude --worktree feature-test.claude --worktree bugfix-test.git worktree list.claude --worktree feature-name
Once you understand worktrees, parallel Claude Code development becomes much safer: each session gets its own working area, while Git keeps the history connected.