Claude Code workflow guide
Use Claude Code worktrees to run parallel sessions without edit collisions
Run claude --worktree <name> from the repository root, choosing a different name in each terminal. Each session gets its own branch and edits its own copy of the files in .claude/worktrees/<name>/. Add the worktrees directory to .gitignore. To copy an ignored file such as .env into each checkout, list it in .worktreeinclude.
Last updated · Published
By CWYN · Published · Checked against docs, not run · Anthropic’s guide to claude --worktree <name> (as of )
1. Quick start for two parallel sessions
- Trust the repository once. Run
claudein the repository root, accept the workspace trust dialog, then exit to the shell. - Give each task a named worktree. From the repository root, run
claude --worktree feature-authin one terminal andclaude --worktree fix-loginin another. One session can build the feature while the other fixes the bug, even when both tasks touch the same file. - Install dependencies in each checkout. Run your project’s install command in
.claude/worktrees/feature-auth/, then in.claude/worktrees/fix-login/. Each worktree starts as a fresh checkout. - Decide what to keep on exit. Choose Keep to save the worktree directory and branch. Save the resume command Claude prints. Choose Remove to delete the directory, its branch and the work in them.
Already in a session? Ask Claude to “work in a worktree.” Claude uses EnterWorktree to move the session into a separate checkout.
2. Choose your worktree settings
| Option | What it does | When to use it |
|---|---|---|
--worktree or -w | Creates a checkout on a new worktree-<name> branch. Omit the name and Claude generates one. | Name a task you plan to return to, or let Claude name a short experiment. |
worktree.baseRef | fresh, the default, branches from the remote default branch, usually main. head branches from your current local HEAD. | Choose head when the task needs commits you have yet to push. |
.worktreeinclude | Copies gitignored files that match its patterns into each new worktree. | Bring a local file such as .env or .env.local into each checkout. |
isolation: worktree | Gives a custom subagent its own worktree when set in its frontmatter. | Let a subagent edit its own copy of the files during your session. |
cleanupPeriodDays | A periodic sweep removes worktrees Claude created for subagents and background sessions once they exceed this age. | Set how long to retain those worktrees. |
claude --worktree "#1234" | Creates a worktree for the pull request at .claude/worktrees/pr-1234. | Start from a proposed change. Quote the PR number, or pass a GitHub PR or GitLab MR URL. |
To start worktrees at your local HEAD, put this object in your Claude Code settings. See which settings.json file applies to your project.
{
"worktree": {
"baseRef": "head"
}
}
worktree.baseRef accepts only fresh and head. To start from a specific branch name, create the worktree directly with Git.
3. Worktree vs. branch vs. subagent
- A branch tracks a line of commits. If two terminals use the same working directory, both sessions see the same files.
- A worktree pairs a branch with its own working directory. Each session edits a separate copy of the files, and both worktrees share the repository history and remote.
- A subagent takes on part of a task inside one Claude Code session. Give the subagent a worktree when its edits need a separate checkout.
If you want to stay in the same checkout and just need room in the conversation, see when to use /compact or /clear.
4. Troubleshoot a worktree
Claude exits and asks for workspace trust
Run claude once in the repository and accept the trust dialog. Exit and retry the named worktree command. A non-interactive claude -p --worktree run skips this trust check.
The .env file is missing
Check that Git ignores the file and that the project root contains a .worktreeinclude pattern matching it. The file uses .gitignore syntax. To copy both of these ignored files, add one pattern per line before creating the worktree.
.env
.env.local
Claude copies matching ignored files when it creates a worktree. If a WorktreeCreate hook replaces Git, Claude skips .worktreeinclude.
Claude refuses to create the directory
Check .claude, .claude/worktrees and the target worktree directory for a symlink. If one of those paths is a symlink, remove that link and retry the command.
A worktree is still on disk after a print-mode run
A -p run has no exit prompt and leaves the worktree in place. Review its files and branch before cleaning it up yourself. See how Claude Code print mode works.
I kept a worktree and want to resume it
Use the claude --worktree --resume command Claude printed when you chose Keep. It takes you back to the directory and branch that hold your edits.
5. Claude Code worktree FAQ
Where are Claude Code worktrees stored?
Look under .claude/worktrees/<name>/ at the repository root. For a worktree named feature-auth, the directory is .claude/worktrees/feature-auth/ and the branch is worktree-feature-auth.
Does a worktree start from my current branch?
By default Claude starts the new branch at the remote default branch. Set worktree.baseRef to head to start at the current local commit and include your unpushed commits.
How do I clean up a worktree?
Exit the interactive session. Claude removes a clean unnamed worktree and its branch automatically. A named worktree prompts first, even if clean. Changes, untracked files or new commits also trigger a prompt. Choose Keep to preserve the directory and branch or Remove to delete both and their work.
Can subagents use worktrees?
Yes. Ask Claude to “use worktrees for your agents.” For a custom subagent, add isolation: worktree to its frontmatter so it gets its own checkout.