Claude Code can start a session in its own git worktree, so two or five sessions can edit the same repository at once without touching each other's files. Each one gets a separate checkout on its own branch; all of them share the repository's history and remote.
This guide covers the --worktree flag and where it puts things, getting a fresh worktree ready to run, choosing what it branches from, isolating subagents, what happens to the worktree when you exit, and the errors you are most likely to hit. Flags and defaults here follow Claude Code's own worktree docs; Claude Code changes often, so check those docs (linked under Sources) against the version you run.
Start a session in a worktree
Pass --worktree (or -w) with a name. Claude Code creates the worktree under .claude/worktrees/<name>/ at the repository root, on a new branch called worktree-<name>, and starts the session there:
claude --worktree feature-auth
# worktree: .claude/worktrees/feature-auth/
# branch: worktree-feature-authRun the same command with another name in a second terminal to start a second isolated session. Leave the name out and Claude Code makes one up, such as bright-running-fox. Passing a name that already exists reopens that worktree instead of creating a new one.
Add the worktree folder to .gitignore, or every session's files show up as untracked in your main checkout:
echo '.claude/worktrees/' >> .gitignoreInteractive runs need workspace trust. If you have never runclaudein this directory, run it once to accept the trust dialog, or--worktreeexits with an error. Non-interactive runs (claude -p --worktree) skip the check.
You can also ask Claude to "work in a worktree" partway through a session. It creates one with its EnterWorktree tool and moves the session into it.
Get the worktree ready: dependencies and .env files
A worktree is a fresh checkout of tracked files. Installed dependencies, build output and gitignored files such as .env are not there. Ask Claude to install dependencies, or run your setup yourself in the worktree's directory.
To copy gitignored files into every worktree Claude Code creates, add a .worktreeinclude file at the project root. It uses .gitignore syntax, and only files that match a pattern and are also gitignored get copied, so tracked files are never duplicated:
.env
.env.local
config/secrets.jsonIt applies to --worktree sessions, subagent worktrees and parallel sessions in the desktop app. It is not read when a WorktreeCreate hook replaces Claude Code's worktree creation; copy the files in the hook instead.
Choose what the worktree branches from
By default a new worktree branches from the repository's default branch on the remote (usually origin/main), not from whatever you have checked out. The worktree.baseRef setting changes that, and takes one of two values:
| worktree.baseRef | New worktrees start from | Use it when |
|---|---|---|
"fresh" (default) | The remote's default branch, fetched if the repository hasn't been fetched in the last 24 hours | Each session should start clean, matching the remote |
"head" | Your current local HEAD, including unpushed commits | Sessions or subagents need to build on work in progress |
{
"worktree": {
"baseRef": "head"
}
}baseRef can't name a branch. To start a session on a specific existing branch, create the worktree with git yourself (below). With no remote configured, worktrees fall back to your local HEAD.
To review a pull request in its own worktree, pass its number with a #, or its URL. Quote it so the shell doesn't treat # as a comment. Claude Code fetches the pull request's head commit from origin and creates .claude/worktrees/pr-<number>:
claude --worktree "#1234"Give subagents their own worktree
Subagents that edit files in parallel can collide just like sessions can. Ask Claude to "use worktrees for your agents", or make it permanent for a custom subagent with isolation: worktree in its frontmatter:
---
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.Subagent worktrees use the same base as --worktree. When a subagent finishes with no changed files, untracked files or new commits, Claude Code can remove its worktree straight away; one with work in it is kept until a periodic sweep can remove it without losing anything. The sweep's age limit is your cleanupPeriodDays setting.
What happens to the worktree when you exit
When you leave an interactive worktree session, Claude Code checks for work that removing the worktree would destroy, then:
| State of the worktree | What Claude Code does |
|---|---|
| Clean, unnamed session | Removes the worktree and its branch without asking |
| Clean, named session | Asks first, so you can keep it for later |
| Changed or untracked files, or new commits | Asks whether to keep or remove it; removing deletes the directory, the branch and the work in them |
| Another session is using it | Keeps it and prints Worktree kept at |
| Its state can't be checked | Asks, and says what it couldn't check |
Non-interactive runs with -p have no exit prompt, so their worktrees are left on disk, along with the git worktree lock Claude Code took when it created them. Remove them with git, unlocking first if git refuses:
git worktree list
git worktree unlock .claude/worktrees/feature-auth # only if remove says it is locked
git worktree remove .claude/worktrees/feature-auth
git branch -d worktree-feature-authgit worktree remove refuses a worktree with uncommitted or untracked files unless you add --force, and it leaves the branch in place. The git worktree remove guide covers both.
Resume a session in a kept worktree
From the main checkout, pass the worktree's name and the session ID Claude Code printed when you exited:
claude --worktree feature-auth --resume <session-id>The name is the folder name under .claude/worktrees/, with / in place of any +. If the worktree directory has since been deleted, the session resumes in the directory you launched from and Claude Code tells you the worktree is gone.
Use git worktree add instead
Create the worktree yourself when you want a specific existing branch or a location outside the repository, then start Claude inside it:
git worktree add -b feature-a ../project-feature-a # new branch
git worktree add ../project-bugfix fix-issue-456 # existing branch
cd ../project-feature-a
claudeClaude Code's exit prompt and periodic sweep don't remove worktrees you created with git worktree add outside .claude/worktrees/, so remove those yourself when you are done. The git worktree add guide covers every form of the command.
Either way, a worktree shares the repository's .git directory with the main checkout. Claude Code also shares project-scope plugins with it, and a "don't ask again" approval for a Bash command made in a worktree session is saved to the main checkout's .claude/settings.local.json, so it applies in every worktree of the repository.
Common errors
Can only use --worktree in a git repository: the directory isn't a git checkout. Rungit initand make a first commit, startclaudewithout--worktree, or, for SVN, Perforce or Mercurial, set upWorktreeCreateandWorktreeRemovehooks.- Git LFS files are pointer files in the worktree: Claude Code skips filter drivers defined in the repository's own
.git/config(asgit lfs install --localsets up) when it creates a worktree. Rungit lfs pullinside the worktree. belongs to a different repositoryoris a leftover worktree: git ties that name's directory to another location, often after a project was copied or moved. Pass a different name.- Uncommitted work keeps a worktree from being removed: commit or stash it, or run
git worktree remove --forceonce you are sure you don't need it. - Claude has no
EnterWorktreetool:permissions.blockWritesOutsideWorkingDirectoriesis on in one of your settings files. Start the session withclaude --worktreeinstead, or remove the key and restart.
Frequently asked questions
Where does Claude Code put its worktrees?
Under .claude/worktrees/<name>/ at the repository root, each on a branch named worktree-<name>. Add .claude/worktrees/ to .gitignore. A WorktreeCreate hook can place them somewhere else.
Does Claude Code delete the worktree when I exit?
Only when it can do so without losing work, or when you say so. A clean worktree from an unnamed session is removed with its branch; anything with changes or new commits prompts you to keep or remove it. Runs with -p never clean up their worktrees.
Why is my .env file missing in the worktree?
A worktree only contains tracked files, and .env is usually gitignored. List it in a .worktreeinclude file at the project root and Claude Code copies it into each new worktree.
Can I start a Claude Code worktree from my current branch?
Yes: set worktree.baseRef to "head" and new worktrees branch from your local HEAD. To start from some other existing branch, create the worktree with git worktree add and run claude inside it.
Do worktrees stop sessions from editing my main checkout?
In an isolated session, Claude Code blocks file edits that target the main checkout, commands whose working directory is the main checkout, and git commands redirected into it. It doesn't track every file a shell command writes, so a cp or shell redirect into the main checkout is governed by your normal permission and sandbox settings instead.
Related guides
- git worktree add: every form of the command, with examples
- git worktree remove: delete a worktree, its branch and what's left behind
Sources
- Run parallel sessions with worktrees (Claude Code docs)
- Subagents (Claude Code docs)
- git-worktree documentation (git-scm.com)
More on worktrees in Git Worktrees, or browse all guides.





