Claude Code worktrees: run parallel sessions without collisions
A git worktree is a second working directory with its own files and branch, sharing your repository's history. Claude Code creates one for you: run claude --worktree feature-auth and it starts a session inside .claude/worktrees/feature-auth on a new branch called worktree-feature-auth. Start another terminal with a different name and the two sessions edit separate copies of the files, so a feature build and a bug fix cannot overwrite each other.
What is a git worktree, and why use one with Claude Code?
A git worktree is a separate working directory with its own files and its own branch. It shares the repository history and remote with your main checkout, so it is not a second clone. The Claude Code worktrees page states the reason to combine them: each session gets a separate copy of the files to edit, so one session can build a feature while a second fixes a bug.
Without a worktree, two Claude sessions in one folder edit the same files, run the same dev server and fight over the same branch. With one, each session has its own folder and branch, and you decide later what to merge. The docs place worktrees among several ways to run Claude in parallel. They isolate file edits. Subagents split work inside one session (see the subagents guide), and cross-session messaging lets Claude pass findings between the sessions in your worktrees.
How do you start Claude in a worktree?
Pass --worktree, or -w, with a name. By default Claude Code creates the worktree under .claude/worktrees/<name>/ at your repository root, on a new branch named worktree-<name>, and starts Claude in it. If you leave the name off, Claude picks one such as bright-running-fox.
# Terminal 1: build a feature cd my-app claude --worktree feature-auth # Terminal 2: fix a bug at the same time cd my-app claude --worktree fix-footer # See every worktree git knows about git worktree list
Four requirements and tips from the docs:
- The repository needs at least one commit. In a repository with no commits the command fails with Failed to resolve base branch "HEAD".
- It must be a git repository. Otherwise you see Can only use --worktree in a git repository. Run git init, make a first commit and retry.
- Accept the trust dialog first. Interactive runs require workspace trust. If you have never run Claude in the folder, run claude there once, then use --worktree. Runs with -p skip the check.
- Add .claude/worktrees/ to .gitignore so worktree contents do not show up as untracked files in your main checkout.
You can also ask for one mid-session. Tell Claude to work in a worktree and it uses its EnterWorktree tool. Add --tmux to claude -w feature-auth to also create a tmux session for the worktree (it uses iTerm2 native panes when available).
How do you set up .env files and dependencies in a new worktree?
A worktree is a fresh checkout. Gitignored files such as .env are not there, and dependencies are not installed. Two fixes cover it.
For files, add a .worktreeinclude file to your project root. It uses .gitignore syntax, and Claude Code copies files that match a pattern and are also gitignored into every worktree it creates. Tracked files are never duplicated.
.env .env.local config/secrets.json
Keep it to small files. It copies rather than links, so putting node_modules in it would mean a full copy per worktree. For large directories use the worktree.symlinkDirectories setting, which symlinks directories from the main repository into each worktree. For installs, either ask Claude to run your install command or keep the command in CLAUDE.md so Claude runs it whenever it starts in a new checkout.
{
"worktree": {
"symlinkDirectories": ["node_modules", ".cache"]
}
}Be careful with .env copies: they put secrets in another folder. If your project reads secrets from files, check your permission rules cover the worktree too (see the permissions guide).
What does a full parallel workflow look like, from start to merge?
Say you are building an app from an idea and want the login feature and a layout fix going at once. This is the sequence with exact commands. Git commands are standard; the Claude Code parts come from the docs.
- Check your repository has a commit and is clean. Run git status and git log --oneline -1.
- Start session one with claude --worktree add-login. Describe the feature and let Claude work. Ask it to install dependencies if needed.
- Open a second terminal in the same repository and run claude --worktree fix-layout. Describe the fix.
- Review each branch separately. From the main checkout, git diff main...worktree-add-login shows what session one changed. Run each app version to check it.
- Push and open pull requests from each worktree, using the branch names worktree-add-login and worktree-fix-layout, for example git push -u origin worktree-add-login.
- Exit each session. Claude checks the worktree for work removal would delete and asks what to do (next section).
Worktrees remove file collisions during the work. They do not remove merge conflicts afterward. If both branches touch the same lines, git still asks you to resolve them when you merge, so give each session a different part of the codebase where you can.
If you are still looking for something to build in parallel, the developer tools ideas on the board each come with a generated build prompt. Paste one into each session.
How do you clean up worktrees and resume them?
When you exit an interactive worktree session, Claude Code checks the worktree for changed or untracked files, uncommitted work inside checked-out submodules, and new commits.
| State of the worktree | What Claude Code does |
|---|---|
| Clean, unnamed session | Removes the worktree and its branch automatically |
| Clean, named session | Asks first, so you can keep the worktree for later |
| Has changes or commits | Asks whether to keep or remove it. Keeping preserves the directory and branch. Removing deletes both, with the work in them |
| State cannot be verified | Asks instead of removing it automatically |
To come back to a kept worktree, run the claude --worktree <name> --resume command that Claude Code prints on exit. Resuming a session that ended inside a worktree returns it to that worktree. If the directory no longer exists, Claude Code resumes in the folder you launched from and tells you the worktree is gone. Calling claude --worktree with a name that already exists opens the existing worktree rather than creating a new one.
Runs with -p have no exit prompt, so Claude does not clean up their worktrees. Remove those yourself:
# List worktrees git worktree list # Remove one (add --force if it has uncommitted changes) git worktree remove .claude/worktrees/add-login # If git says it is locked, pass --force twice (git's rule for locked worktrees) git worktree remove --force --force .claude/worktrees/add-login
Worktrees created for subagents and background sessions are swept automatically once they are older than your cleanupPeriodDays setting and hold no work. The sweep leaves a worktree in place if it still has changed or untracked files or unpushed commits, or if you created it yourself with git worktree add.
Which branch does a worktree start from, and can it start from a pull request?
New worktrees branch from the repository's default branch on the remote, usually main. This is the worktree.baseRef setting with its default of fresh. Set it to head to branch from your current local HEAD instead, so the worktree carries your unpushed commits. You cannot set the value to a branch name. To start from a specific existing branch, create the worktree with git directly (see below).
{
"worktree": {
"baseRef": "head"
}
}To work on someone's pull request, pass the number with a hash, a GitHub pull request URL, or a GitLab merge request URL. Claude Code fetches the head commit from origin and creates the worktree at .claude/worktrees/pr-<number>. Quote the argument so your shell does not read the hash as a comment.
claude --worktree "#1234"
Can you create worktrees with plain git instead?
Yes. Do this when you need an existing branch checked out or want the worktree outside the repository. Then start Claude in the folder as usual.
# New worktree on a new branch git worktree add ../project-feature-a -b feature-a # Worktree from an existing branch git worktree add ../project-bugfix fix-issue-456 # Start Claude inside it cd ../project-feature-a claude # List and remove git worktree list git worktree remove ../project-feature-a
Can subagents run in their own worktrees?
Yes. Ask Claude to use worktrees for its agents, or set isolation: worktree in a custom subagent's frontmatter. Each subagent gets a temporary worktree that Claude Code removes when the subagent finishes without changes. This subagent file, from the docs, always runs in its own 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.
For one large mechanical change, the docs also mention /batch, which splits work into 5 to 30 units, shows you the plan, then runs each unit in its own worktree and opens a pull request for it. Subagent worktrees branch from the default branch unless worktree.baseRef is head.
What do the common worktree errors mean?
| Message or symptom | What to do |
|---|---|
| Can only use --worktree in a git repository | Run git init and make a first commit, set up a WorktreeCreate hook for another version control system, or start Claude without --worktree |
| Failed to resolve base branch "HEAD" | The repository has no commits yet. Make one and retry |
| Worktree creation fails on a symlinked path | Claude Code refuses when .claude, .claude/worktrees or the worktree folder is a symlink. Remove the symlink and retry |
| git refuses to remove a worktree | It is locked. Release the lock, or pass --force twice to git worktree remove |
| --worktree exits asking you to trust the folder | Run claude once in the directory and accept the trust dialog |
Worktree isolation uses git by default. For SVN, Perforce, Mercurial and others, you configure WorktreeCreate and WorktreeRemove hooks to supply your own creation and cleanup.
Frequently asked questions
How do I run multiple Claude Code sessions at the same time?
Start each in its own git worktree with claude --worktree and a different name per terminal, for example claude --worktree feature-auth and claude --worktree fix-footer. Each session edits its own copy of the files on its own branch, so they cannot overwrite each other. You can also launch separate sessions with plain git worktree add.
Where does Claude Code create worktrees?
By default under .claude/worktrees/<name>/ at your repository root, on a new branch called worktree-<name>. Add .claude/worktrees/ to your .gitignore so the contents do not appear as untracked files. To place worktrees elsewhere, create them yourself with git worktree add or configure a WorktreeCreate hook.
Does a Claude Code worktree include my .env file?
Not by default. A worktree is a fresh checkout, so gitignored files such as .env are missing. Add a .worktreeinclude file at the project root listing the files to copy, using .gitignore syntax. Only files that match and are also gitignored are copied. For large folders such as node_modules, use worktree.symlinkDirectories instead.
How do I delete a Claude Code worktree?
On exit, Claude removes a clean unnamed worktree automatically and asks about named ones or ones with changes. To delete one yourself, run git worktree remove <path>, adding --force if it has uncommitted changes. If git reports it is locked, release the lock first or pass --force twice. Runs started with -p are never cleaned up automatically.
Do Claude Code worktrees prevent merge conflicts?
No. They prevent two sessions from editing the same files at the same time. When you merge the branches, git can still report conflicts if both changed the same lines. Give each session a different part of the codebase where possible, merge one branch first, and rebase the next.
Can Claude Code start a worktree from a pull request?
Yes. Run claude --worktree "#1234", or pass a GitHub pull request URL or GitLab merge request URL. Claude Code fetches that change's head commit from origin and creates the worktree at .claude/worktrees/pr-1234. Quote the hash so your shell does not treat it as a comment.
Created with AI assistance and edited for this site. Examples are illustrative and do not represent customer research. About the publisher.