Tested on Claude Code 2.1.332 in throwaway git repos. Three ways to give a session its own worktree were run against the same repo: a plain git worktree add, the EnterWorktree tool (a deferred tool) called mid-session, and the claude -w <name> flag. Each session ran the same Bash command (pwd, branch, last commit, files, git status). Results are from that output, git worktree list, du, the result JSON of claude -p, and the session transcripts.
The repo was set up so the three ways can differ: origin/main had commit B, the local main had an unpushed commit C instead, there was an uncommitted edit and an untracked file, a gitignored .env, a .worktreeinclude naming .env, and a gitignored .claude/settings.local.json.
The three ways side by side
| git worktree add | EnterWorktree tool | claude -w |
|---|
| Folder | where you say | <repo>/.claude/worktrees/<name> | <repo>/.claude/worktrees/<name> |
| Branch | where you say | worktree-<name> | worktree-<name> |
| Starts from | local HEAD (commit C) | origin/main (commit B) | origin/main (commit B) |
| Unpushed commit C | present | absent | absent |
| Uncommitted edit, untracked file | absent | absent | absent |
Gitignored .env | absent | copied (named in .worktreeinclude) | copied (named in .worktreeinclude) |
.claude/settings.local.json | not in the folder, settings still applied | not in the folder, settings still applied | not in the folder, settings still applied |
| git lock | none | claude session <name> (pid ... start ...) | claude session <name> (pid ... start ...) |
| Git guard on Bash | none | yes (below) | yes (below) |
| After the session ends | stays | stays, locked | stays, locked |
--resume starts in | the folder you start from | the worktree | the worktree |
| Size on disk | 20 KB | 24 KB | 24 KB |
| Time to make it | 0.014 s | 0.195 s (tool call to result) | inside start-up |
claude -p turns for the task | 2 | 10 | 8 |
| Cost of the run | $0.033 | $0.056 | $0.043 |
The main difference: the Claude Code ways start the branch from the remote's default branch, not from where you are. Work that is not pushed is not in the worktree.
The git guard
In a session that is in a Claude Code worktree, a Bash command that names git in a way Claude Code cannot check is refused:
This session is isolated in the worktree <repo>/.claude/worktrees/flagwt, but this command names git in a form too complex to verify that it stays inside the worktree. Refusing to run it — a worktree-isolated session's git operations must target its own worktree. Split it into plain, separate commands and run them from <repo>/.claude/worktrees/flagwt.
A compound command such as pwd; git branch --show-current; git log -1 was refused in all three Claude Code worktree runs, so the model split it into single commands. That is where the extra turns and cost come from. A plain git worktree has no guard.
Resume goes back into the worktree
The transcript holds a worktree-state entry:
"type":"worktree-state","worktreeSession":{"originalCwd":"<repo>","preEnterOriginalCwd":"<repo>","worktreePath":"<repo>/.claude/worktrees/probe","worktreeName":"probe","worktreeBranch":"worktree-probe","originalBranch":"master","originalHeadCommit":"<sha>", ...}A resumed session reads it and starts in the worktree again: pwd printed the worktree path before and after --resume, for the tool and for -w. A restarted agent stays in its worktree until it calls ExitWorktree. Starting it from another folder does not change this. The transcript file follows the session: while it is in the worktree, it is written under the worktree's project folder (-<repo>--claude-worktrees-<name>), and after ExitWorktree it is under the repo's project folder again.
EnterWorktree and ExitWorktree
EnterWorktree returned:
Created worktree at “<repo>/.claude/worktrees/toolwt” on branch “worktree-toolwt”. The session is now working in the worktree. Use ExitWorktree to leave mid-session, or exit the session to be prompted.
ExitWorktree with action: remove returned:
Exited and removed worktree at “<repo>/.claude/worktrees/toolwt”. Session is now back in “<repo>”.
It deleted the folder and the worktree-toolwt branch. git worktree list and git branch no longer showed either.
Subagent worktrees (Agent with isolation: "worktree")
Measured with two subagents in one claude -p session, on the same repo setup (unpushed local commit C, origin/main at B).
| Subagent that changed nothing | Subagent that wrote a file |
|---|
| Folder | <repo>/.claude/worktrees/agent-<agent id> | <repo>/.claude/worktrees/agent-<agent id> |
| Branch | worktree-agent-<agent id> | worktree-agent-<agent id> |
| Starts from | origin/main (B) | origin/main (B) |
| After the subagent ends | folder and branch removed | folder and branch kept; the Agent result adds worktreePath: and worktreeBranch: |
| git lock | none | none |
| Run time | 2.9 s | 2.7 s |
What the git guard blocks
One command per Bash call, in a claude -w guard session:
| Command | Result |
|---|
git status --short | ran |
git -C <repo> log -1 | refused: this command redirects git to the shared checkout via -C |
cd <repo> && git log -1 | refused: this command changes directory to the shared checkout (<repo>) before running git |
git --git-dir=<repo>/.git log -1 | refused: this command redirects git to the shared checkout via --git-dir |
cd <repo> && git commit --allow-empty -m escaped | refused (as cd above); the main checkout's log shows no such commit |
git checkout main | not refused by the guard; git itself refused: 'main' is already used by worktree at '<repo>' |
git switch -c other | ran; the worktree moved to branch other |
git commit --allow-empty -m guardtest | ran |
git push origin HEAD:refs/heads/guardpush | ran; the remote got the branch guardpush |
The guard stops git from reaching the main checkout. It does not limit what git does inside the worktree, including a push. A compound command it cannot parse is refused as well (above).
What the lock blocks
After the -w session ended (its process gone), its worktree was still locked:
| Command | Result |
|---|
git worktree remove <path> | fatal: cannot remove a locked working tree, lock reason: claude session guard (pid ... start ...) |
git worktree remove -f <path> | the same |
git worktree prune | kept it |
git worktree remove -f -f <path> | removed it |
The lock outlives the session. A cleanup script that does not use -f -f cannot remove the worktree.
WorktreeCreate and WorktreeRemove hooks
A project .claude/settings.json with a WorktreeCreate hook replaces Claude Code's own git step. The hook gets this on stdin:
{"session_id":"<id>","transcript_path":"<path>","cwd":"<repo>","hook_event_name":"WorktreeCreate","name":"hooked"}It makes the worktree itself and prints its path on stdout. The test hook ran git worktree add <custom dir>/<name> -b custom-<name> HEAD. With it:
claude -w hooked started in <custom dir>/hooked on branch custom-hooked, at local commit C, so the unpushed work was there.- A subagent with
isolation: "worktree" also went through the hook: folder <custom dir>/agent-<agent id>, at commit C. ExitWorktree with remove first refused: Could not verify worktree state at “<custom dir>/hooked”. Refusing to remove without explicit confirmation. .... With discard_changes: true it called the WorktreeRemove hook, with stdin {..., "hook_event_name":"WorktreeRemove","worktree_path":"<custom dir>/hooked"}, and reported the worktree removed. The test hook did nothing, so the folder and branch were still there: removal is the hook's job.- The subagent's worktree was kept after it ended, though it changed nothing, and
WorktreeRemove was not called for it.
So a WorktreeCreate hook can make every Claude Code worktree start from local HEAD, through the repo's own script. Cleanup is then the hook's job too.