Claude Code worktrees: isolated files and shared content
Inspect --worktree directories, branches and dependencies, and separate file copies, shared Git history and exit cleanup.
Directories and branches belong to one repository · A fresh directory still needs an environment · Inspect retained work at exit
At a glance
The goal is to understand how file edits in two Claude Code sessions are separated. The official worktree page was checked; no worktree, dependency installation or session was created. This covers Git worktrees; desktop and other version-control setups need their own documented entry.
Runtime and applicability
- Article type
- Spec file
- Verified
- Applies to
- Claude Code Applies to Claude Code as documented; version, model and environment prerequisites are stated in the body.
- Tools
File locations
| Path | Scope | How it is read |
|---|---|---|
.claude/worktrees/<name>/ | A worktree file copy and its separate branch. | claude --worktree <name> creates the checkout and starts the session there. |
When to use
- When two tasks need separate editable files and separately inspected development environments.
When not to use
- For blocking network, external services or shared credentials, configure permissions and runtime separately.
Directories and branches belong to one repository
The docs describe a Git worktree as a separate directory with its own files and branch, sharing history and remotes with the main repository. --worktree or -w accepts a name; the default location is .claude/worktrees/<name>/ with branch worktree-<name>. The repository needs an existing commit. File copies separate edits but do not establish isolated remote services, build ports or permissions.
A fresh directory still needs an environment
A worktree is a fresh checkout and needs dependency initialization there. The documented .worktreeinclude can copy gitignored files, including the page’s .env example. Inspect each intended file before adopting that copying to avoid distributing credentials inadvertently. Interactive startup needs workspace trust; the docs say -p worktree runs skip that trust check. This is not a recommendation to bypass approvals or trust inspection.
Inspect retained work at exit
Cleanup checks changed and untracked files, submodule work and new commits. Interactive sessions with work prompt to keep or remove the worktree. Non-interactive -p runs have no exit prompt and do not clean it up automatically. Terminal exit does not prove directory removal. Inspect actual Git state and files to retain before following cleanup or resume instructions; do not simply delete a directory containing work.
Verification scope and stop conditions
Only documentation and example path string structure were checked. Git metadata, session resume, cleanup and copying were not verified. Before adoption, inspect launch directory, branch, dependencies and shared-service configuration, then check that edits remain separated. Stop cleanup when worktree identity is unclear, files are unsaved or resume points to an unexpected location; preserve state for diagnosis. A worktree does not replace a sandbox.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.