Running two Claude Code sessions in the same folder is a recipe for chaos: one session rewrites a file while the other is halfway through editing it. Git worktrees solve this.
Each session gets its own copy of the files on its own branch, sharing the same repository history, so you can have Claude fixing a bug in one terminal while it builds a feature in another.
Claude Code has worktrees built in. One flag, --worktree, creates the worktree, starts the session in it, keeps edits isolated from your main checkout and offers to clean up when you exit. You can also isolate subagents in their own worktrees and start straight from a pull request.
This guide covers starting a session in a worktree, setting up the environment, cleanup and resuming, isolation rules, subagent worktrees, choosing the base branch, copying .env files with .worktreeinclude, and fixes. It follows Anthropic’s official Claude Code documentation as of December 2026.
- Start:
claude --worktree feature-auth(or-w). Creates.claude/worktrees/feature-auth/on branchworktree-feature-auth. - Parallel: run it again with another name in a second terminal.
- From a PR:
claude --worktree "#1234" - Copy .env: list gitignored files in a
.worktreeincludefile. - Subagents: add
isolation: worktreeto the agent’s frontmatter. - Ignore: add
.claude/worktrees/to.gitignore.
Why Use Worktrees With Claude Code?
A git worktree is a separate working directory with its own files and branch that shares the repository’s history and remote with your main checkout. Claude Code offers a few ways to work in parallel, and worktrees are the one that isolates file edits:
| Approach | What it does | Use when |
|---|---|---|
| Worktrees | Separate files and branch per session | Two or more sessions editing code at once |
| Subagents | Split work inside one session | Research or parallel subtasks; see our subagents guide |
| Cross-session messaging | Sessions pass findings to each other | Coordinating separate sessions |
Worktrees need a git repository. For other version control systems, the docs describe WorktreeCreate and WorktreeRemove hooks that replace the git logic. The desktop app has its own worktree option per session; see our Claude Code desktop app guide.
Start Claude in a Worktree
- Trust the repo once. If you have never run Claude there, run
claudein the repo and accept the trust dialog. Otherwise--worktreeexits with an error. - Start the session:
claude --worktree feature-auth. The worktree is created at.claude/worktrees/feature-auth/on a new branchworktree-feature-auth. - Start a second one in another terminal:
claude --worktree bugfix-123. Leave out the name and Claude generates one likebright-running-fox. - Ignore the folder: add
.claude/worktrees/to.gitignoreso worktree files do not show up as untracked in your main checkout.
You can also ask Claude mid-session to “work in a worktree”. It creates one with its EnterWorktree tool and can switch between worktrees under .claude/worktrees/. Moving to a path outside that folder asks for your approval first.
Set Up the Worktree Environment
A worktree is a fresh checkout. Dependencies such as node_modules and gitignored files such as .env are not there.
- Ask Claude to install dependencies, or run your setup in the worktree folder yourself.
- Create a
.worktreeincludefile at the project root to copy gitignored files into every new worktree automatically:
.env
.env.local
config/secrets.json
It uses .gitignore syntax, and only files that match a pattern and are gitignored are copied, so tracked files are never duplicated. It applies to --worktree sessions, subagent worktrees and parallel desktop sessions. Keep secrets out of Claude’s reach with deny rules too; our Claude Code permissions guide shows how.
Hooks note: after Claude enters a worktree, ${CLAUDE_PROJECT_DIR} in your hooks still points at the main project, while the cwd field in the hook input follows the worktree. See our hooks guide.
Clean Up When You Exit
When you exit an interactive worktree session, Claude checks for changed or untracked files, uncommitted submodule work and new commits:
| State | What happens |
|---|---|
| Clean, unnamed session | Worktree and branch are removed automatically |
| Clean, named session | You are asked first, so you can keep it |
| Has work in it | You choose keep or remove. Remove deletes the folder, branch and all work |
| Another session is using it | It is kept, with a Worktree kept at message |
| State cannot be verified | You are asked rather than auto-removed |
Gitignored files do not count as work, so a .env you created in the worktree can be deleted on exit. Copy out anything you need first.
Non-interactive -p runs have no exit prompt and leave their worktrees; remove them with git worktree remove (add --force for uncommitted changes, and git worktree unlock first if it is locked).
Worktrees Claude made for subagents and background sessions are swept automatically once they are older than cleanupPeriodDays, unless they still hold work.
Resume a Worktree Session
To return to a worktree you kept, run this from the main checkout with the session ID printed on exit:
claude --worktree feature-auth --resume <session-id>
- A plain
--resumeof a session that ended isolated in its worktree returns it to that worktree. --continuepicks the most recent session recorded under the directory you launch from.--fork-sessionstarts the fork in your launch directory and leaves the original worktree alone.- If the worktree was deleted, the session resumes in your launch directory and tells you.
How Isolation Works
While a session is isolated in a worktree, Claude Code blocks it, and every subagent it starts, from touching the main checkout:
- File edits to paths in the main checkout are refused.
- Commands whose working directory is the main checkout are refused.
- Git redirects such as
git -C,--git-dirorGIT_DIRpointing at the main checkout are refused. - Unverifiable commands where Claude Code cannot tell whether git stays inside the worktree are refused.
These checks read paths and command text. They do not track every file a shell command writes, so a cp or redirect into the main checkout is not caught. Claude sees each refusal as an error explaining how to proceed. For OS-level limits, combine worktrees with the Claude Code sandbox.
Isolate Subagents in Worktrees
Subagents can each run in their own worktree so parallel edits do not collide. Ask Claude to “use worktrees for your agents”, or set it in a custom subagent file in .claude/agents/:
---
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.
When the subagent finishes, a clean worktree with no new commits can be removed right away; one with changes stays until the periodic sweep. You can gate this with a permission rule such as Agent(isolation:worktree) in ask.
Base Branch, Pull Requests and Reuse
Choose the base branch
New worktrees branch from the repository’s default branch on the remote, usually main. Set worktree.baseRef in settings to change it:
{
"worktree": {
"baseRef": "head"
}
}
"fresh" (default) starts from a clean copy of the remote default branch. "head" starts from your current local HEAD, carrying unpushed commits, which suits subagents working on in-progress code.
It cannot be set to a branch name; for a specific branch, create the worktree with git worktree add yourself. More on settings in our Claude Code settings guide.
Start from a pull request
claude --worktree "#1234"
claude --worktree https://github.com/org/repo/pull/1234
Claude Code fetches the PR head from origin (pull/<n>/head on GitHub, merge-requests/<n>/head on GitLab). The fetch never prompts, so your SSH key or credential helper must work without a password prompt. Great for reviewing a PR without disturbing your own branch.
Reuse a name
Passing an existing name reopens that worktree. If it is clean, on its own branch and its work has been merged, Claude Code can reset it to the latest default branch instead of reusing the old tip.
A Practical Parallel Workflow
- Terminal 1:
claude -w feature-searchand describe the feature. Start in plan mode for anything big. - Terminal 2:
claude -w fix-loginfor an urgent bug. - Terminal 3:
claude -w "#482"to review a teammate’s PR. - Review each branch with
git log worktree-fix-loginor in your editor, then ask Claude to commit and open a PR. - Exit each session and choose keep or remove.
Each session uses its own context and usage, so three parallel sessions cost roughly three times as much. Our token usage guide helps keep that in check.
Troubleshooting
–worktree exits with a trust error
Run claude once in the repository and accept the workspace trust dialog.
The app fails because .env is missing
Add the file to .worktreeinclude, or copy it in manually. Worktrees only contain tracked files.
Claude has no EnterWorktree tool
permissions.blockWritesOutsideWorkingDirectories is on. Start with claude --worktree instead, or remove the key and restart. Run /status to see which settings files set it.
A command was “blocked by the worktree isolation checks”
Claude tried to run git or edit files in the main checkout. Let it work inside the worktree, or make that change yourself in the main checkout.
Worktrees pile up
Run git worktree list, then git worktree remove on old ones. Subagent and background worktrees are swept after cleanupPeriodDays.
PR fetch fails
The fetch cannot prompt for a password or SSH passphrase. Load your key into ssh-agent or set up a credential helper.
FAQ
Where does Claude Code put worktrees?
Under .claude/worktrees/<name>/ at the repository root, on a branch named worktree-<name>.
Do worktrees copy node_modules?
No. Install dependencies in each worktree, or ask Claude to.
Can I use worktrees without git?
Only with WorktreeCreate and WorktreeRemove hooks that replace the git logic for your VCS.
Will exiting delete my work?
Only if you choose remove. Clean unnamed worktrees are removed automatically, but uncommitted work always prompts. Gitignored files are not counted, so copy those out.
Can two sessions share one worktree?
They can, but that defeats the point. Claude keeps a worktree that another session is using.
Do worktrees work in the VS Code extension?
Ask Claude to work in a worktree in any session, or start claude --worktree from VS Code’s terminal.




