Claude Code worktrees: run parallel sessions with --worktree, set them up, and clean them up
Claude Code worktrees: start a session in its own checkout with --worktree, carry .env over, clean up, and see every worktree's sessions and diff in Caprock.
A git worktree is a second checkout of the same repository, on its own branch. claude --worktree <name> (or -w) creates one under .claude/worktrees/<name>/ on a new branch worktree-<name> and starts Claude Code in it, so two sessions can edit one repository without touching each other's files. Run it again with another name in another terminal for a third.
Start Claude Code in a worktree
- 1
A named worktree, on a new branch worktree-feature-auth
- 2
Let Claude Code pick a name, such as bright-running-fox
- 3
Branch from pull request 1234, fetched from origin
- 4
Open the worktree in a tmux session (iTerm2 panes where available)
Inside a session you can also ask Claude to "work in a worktree"; it creates one with its EnterWorktree tool.
- Trust first. An interactive
--worktreerun needs the folder trusted: runclaudeonce in the repository and accept the dialog, or--worktreeexits with an error.claude -p --worktreeskips the check. - Keep it out of git status. Add
.claude/worktrees/to.gitignore. - A name that exists opens that worktree again instead of making a new one; with the default base, a clean one with no commits of its own restarts from the default branch.
Set up the worktree: .env and dependencies
A worktree holds only tracked files. Your .env, local config and installed dependencies are not there. For ignored files, add a .worktreeinclude at the project root, in .gitignore syntax:
.env .env.local config/secrets.json
Claude Code copies files that match a pattern and are gitignored into every worktree it creates with git: --worktree ones, subagent worktrees and the desktop app's parallel sessions. Dependencies you install in the worktree yourself, or ask Claude to.
Which branch a worktree starts from
- Fresh, the default (
"fresh"): the remote's default branch, usuallymain. Claude Code fetches it first when the repository has not been fetched in 24 hours. - Head (
"head"): your current localHEAD, with unpushed commits — set"worktree": { "baseRef": "head" }in settings. - A specific branch:
worktree.baseRefdoes not take a branch name; make that worktree with git, below, and startclaudein it.
Subagents in their own worktrees
Add isolation: worktree to a subagent's front matter in .claude/agents/, or ask Claude to "use worktrees for your agents". Each subagent then edits a temporary worktree from the same base branch; one that finishes without changes is removed at once. More on subagents: Claude Code subagents.
Clean up worktrees
When you exit an interactive worktree session, Claude Code checks it first:
- Clean, unnamed session: the worktree and its branch are removed.
- Clean, named session: it asks before removing.
- Changes, untracked files or new commits: it asks you to keep or remove. Keep it and it prints the
claude --worktree <name> --resumecommand to come back.
Runs with -p get no exit prompt and are not cleaned up. Remove a worktree yourself with git worktree remove <path>, adding --force when it has uncommitted changes; if git says it is locked, run git worktree unlock on it first. Worktrees made for subagents and background sessions are swept once they are older than cleanupPeriodDays, unless they still hold work.
Worktrees by hand, with git
For a worktree outside the repository or on an existing branch, use git and start Claude in it:
- 1
A worktree on a new branch, beside the repository
- 2
Or on a branch that already exists
- 3
List them
- 4
Remove one when you are done
What gets hard with several worktrees
Each worktree is a terminal of its own, a branch of its own and a diff to review of its own. With three or four running, you lose track of which one is waiting on you and which has finished, and every review starts with a cd and a git diff. Claude Code's agent view lists background sessions; sessions you started in other terminals are not in it.
Worktrees in the Caprock app
Caprock is a free desktop app for macOS, Windows and Linux that records every agent session on the machine, whether or not it started it, and runs Claude Code, Codex, OpenCode, Gemini CLI, Grok Build and Hermes Agent in terminal tabs, per project.
- Every session, whichever worktree it is in. The sidebar lists a project's agent sessions, each with the branch it works on when that is not the project's own: in worktrees
claude --worktreemade, ones you made with git, and Caprock's own. - A new worktree with an agent in it. New agent → Where → New copy from a branch, and Caprock runs
git worktree addinto.caprock-worktrees/<name>on a new branchcaprock/<name>, from the branch you pick (fetched from the remote first unless you untick it), and starts the agent there. - Ready to run. The new worktree gets the ignored files your
.worktreeincludelists, the wayclaude --worktreedoes, and a project's setup command, such asnpm install, runs in the agent's terminal before the agent starts. - Several tries at once. Set Attempts to 2, 3 or 4 and the same agent gets the same first message in that many worktrees from one commit; Compare attempts shows each one's files and lines changed, cost so far and time.
- A task becomes a worktree. Type a task into the palette (⌘K) that matches nothing — "Fix the login bug" — and Enter opens New agent with it as the first message, in a new worktree named
fix-the-login-bug. - Review each one. Review and commit in a session's inspector, or Review changes in ⌘K, opens its worktree's Changes view: stage files or hunks, commit, push, and with GitHub connected open a pull request and follow its checks. Open in … opens the worktree in VS Code, Cursor, Zed or a JetBrains IDE.
- From the phone. A paired phone you let control sessions can make a worktree — on an existing branch, a new one or a branch only the remote has — and start an agent in it.
- Clean up. Clean up copies… in a project's menu lists every worktree with its branch, size on disk and whether it is merged, pushed or has uncommitted changes; it removes only the ones Caprock made, and asks again before anything unsaved is lost. A copy Caprock made that is merged, pushed and clean can go by itself: Settings → Worktrees offers Off, Ask (the default) or Auto.
Add .caprock-worktrees/ to .gitignore beside .claude/worktrees/. Install Caprock.
Claude Code's worktrees or Caprock's
- Claude Code's
--worktree— one or two parallel sessions in terminals you already have, with cleanup on exit and.worktreeinclude. - Subagents with
isolation: worktree— parallel edits inside one session that report back to it. - Caprock's app — several worktrees and agents across projects in one window, each with its diff a click away, whichever tool made the worktree, and several tries at one task compared by cost. More ways to run agents side by side: run Claude Code agents in parallel.
Questions people ask
- How do I use git worktrees with Claude Code?
- Run claude --worktree <name> (or -w) in your repository. Claude Code creates a worktree under .claude/worktrees/<name>/ on a new branch worktree-<name> and starts the session in it. Run it again with another name in another terminal for a second isolated session.
- Where does Claude Code put worktrees?
- Under .claude/worktrees/<name>/ at the repository root, on a branch named worktree-<name>. Anthropic's docs suggest adding .claude/worktrees/ to .gitignore. A WorktreeCreate hook can put them anywhere else.
- Which branch does a Claude Code worktree start from?
- The repository's default branch on the remote, usually main. Set worktree.baseRef to "head" in settings to branch from your current local HEAD instead; to start from a specific branch, create the worktree with git worktree add.
- Why is .env missing in my Claude Code worktree?
- A worktree is a fresh checkout, so gitignored files are not there. List them in a .worktreeinclude file at the project root and Claude Code copies them into every worktree it creates.
- Does Claude Code delete worktrees automatically?
- On exit, a clean worktree from an unnamed session is removed with its branch; a worktree with changes or commits makes Claude Code ask you to keep or remove it. Runs with -p are not cleaned up; remove those with git worktree remove.
- Can I see all my worktrees and their sessions in one place?
- Caprock's desktop app lists every agent session of a project in its sidebar, with the branch it works on when that is not the project's own, sessions in worktrees claude --worktree made included. Clean up copies lists every worktree of the project with its size on disk and whether each is merged, pushed or has changes.
Sources
- Claude Code docs — Run parallel sessions with worktrees: reusing a name, the fresh base, cleanup, read 20 October 2026
- Claude Code docs — CLI reference: --worktree and --tmux, read 20 October 2026
- Claude Code docs — Agent view: background sessions, read 20 October 2026
- Claude Code docs — Subagents, read 7 October 2026
- Git documentation — git-worktree
- Caprock docs — the desktop app: the sidebar, new copies, Clean up copies, the palette, Changes and pull requests (docs/app.md)
- Caprock source — a new agent's worktree: git worktree add -b caprock/<name> under .caprock-worktrees (internal/agents/worktree.go)
- Caprock source — every linked worktree of a project, read from .git/worktrees (internal/projects/git.go)
- Caprock source — the New agent sheet's New worktree option (ui/src/components/NewAgentSheet.tsx)
- Caprock source — a palette task becomes a worktree name (ui/src/screens/AppShell.tsx)
- Caprock changelog — 0.75.0 projects and worktrees on any branch; 0.77.0 a worktree from the phone; 0.78.0 Changes, pull requests and the palette's new-worktree task; 0.94.0 the sidebar lists sessions, no worktree rows; 0.112.1 finished copies clean themselves up; 0.99.0 Grok Build; 0.122.0 Hermes Agent
Try it
The record of every coding agent you run, the sessions it did not start included. Free, open source; nothing leaves your machine.
Notarized by Apple. One file for Apple silicon and Intel, macOS 13 or later.
Or run it yourself:
Paste in Terminal: it installs Caprock and opens it.Or with Homebrew: brew install --cask dspv/tap/caprock-app