Arguments: $ARGUMENTS. Grammar: [<branch>] [<repo>] [-- <task>].
- branch — optional; the branch name for the new worktree. When omitted,
pick one (step 1 below).
- repo — optional path; create the worktree in this repo instead of the
session's current one.
- task — optional; what to do inside the new worktree. No task means enter
the worktree and wait.
Tokens before the -- are the branch and/or repo: a path-shaped token
(starting with /, ~, ./, or ../) is the repo; any other token is the
branch (docs is a branch name, never the docs/ directory). More than one
branch-shaped token before a -- doesn't fit the grammar — ask. Without a
--, judge where the task starts: leading tokens that read as a branch name
(fix-auth) or a repo path are consumed as such, and the rest is the task;
otherwise the whole input is the task (fix the parser bug has no
branch-shaped lead — all task).
/wt-switch-create my-feature -- fix the parser bug
/wt-switch-create -- fix the parser bug
/wt-switch-create my-feature ~/workspace/other-repo -- fix the parser bug
/wt-switch-create my-feature
What to do
Creating the worktree comes first on every invocation, before any other work.
The invocation is itself the explicit request to create it; a research or
read-only task gets one all the same.
Pick the branch name if none was given: short, from the task and
consistent with existing worktree names, or, mid-session, from the work
being moved; with nothing to derive from, ask.
With no repo argument, create and enter in one call:
EnterWorktree({name: "<branch>"}). Worktrunk's WorktreeCreate hook runs
wt switch --create, so the result is an ordinary wt worktree in the
default layout, and the user sees no confirmation prompt. On success,
do the task (or, with no task text, confirm it's ready and wait).
Mid-session, carry uncommitted work across: git stash push -u before the
EnterWorktree call, then git stash pop after — the call re-roots the
session into the new worktree, and the stash is shared across worktrees.
Otherwise create it with wt and enter by path. Two cases reach here: a
repo argument, which step 2 can't target, and a failed step 2, whose error
says which — ✗ Branch <branch> already exists, or Already in a worktree session. Create with a Bash call (omit -C <repo> for this repo):
wt -C <repo> switch --create <branch> --no-cd --format=json
Stdout is JSON whose path field is the worktree's absolute path (status
lines go to stderr). On Branch <branch> already exists: if the user named
the branch, rerun without --create (it enters the branch, creating its
worktree if missing); if step 1 picked the name, pick another and rerun. Any
other failure (not a git repo, invalid name): report it and stop.
Then call EnterWorktree({path: "<path from the JSON>"}).
- Accepted → the session is re-rooted in the worktree. Do the task (or,
with no task text, confirm it's ready and wait).
- Tool error — the tool ran and returned an error (
Cannot enter worktree: …) → graceful; nothing moved, and one recovery covers them
all. Common causes: the cwd resolves to no git repo (e.g. a non-git
parent like ~/workspace that only holds repos, as in a background job)
or to a different repo than the target; or the session is already rooted
in a worktree (or is a pinned agent), which limits entry to the current
repo's .claude/worktrees/ and excludes even a same-repo wt sibling.
The recovery test is whether you can cd into the worktree, which works
when it's inside an allowed directory. So cd <path> and read the
result:
- no
Shell cwd was reset notice → it stuck; the worktree is reachable.
Work there, but a bare cd is not a tracked re-root, so the cwd can
revert to the session's launch worktree across turns (and in spawned
subagents); pin commands with git -C <path> / wt -C <path> rather
than trusting the cd to persist.
Shell cwd was reset → not reachable. Stop and ask the user to make it
reachable: add the repo, or a parent like ~/workspace, to
permissions.additionalDirectories (durable, every session), or run
/add-dir <path> (this session). Then continue. Don't grind through
absolute paths with cd resetting on every command.
- Denied — the call itself was refused, with no tool error → however
the denial is worded, it is the user's answer to the confirmation Claude
Code shows for entering a worktree outside
.claude/worktrees/, unless
there was no user to ask (the denial says the session couldn't prompt),
which decides nothing — take the recovery above. On the user's answer:
the worktree wt just created still exists; only the entry didn't
happen. Report its path and ask how to proceed, since reaching it
through cd would override that answer.
Cleanup
The worktree is a normal worktrunk worktree: it shows up in wt list and is
merged or removed with wt merge / wt remove <branch> like any other. Don't
remove it unprompted.
A worktree from step 2 that the session never touched — no changed files, no
commits — is cleaned up when the session ends, branch included; anything
written into it keeps it. A worktree from step 3 always stays. If the user asks
to leave mid-session, ExitWorktree({action: "keep"}) returns the session to
its original directory;
ExitWorktree cannot remove a worktree entered by path, so removing one of
those is always wt remove <branch>.
Scope
The command's mandate is ONE worktree (in the named repo, if one was given)
and the requested task inside it. Commits, pushes, and merges still each
require explicit user permission.
1---2name: wt-switch-create3description: Create a new worktrunk worktree (optionally in another repo) and switch this session's working directory into it. Use when launching a session that should work in its own worktree.4license: MIT OR Apache-2.05---6
7Arguments: `$ARGUMENTS`. Grammar: `[<branch>] [<repo>] [-- <task>]`.
8
9- **branch** — optional; the branch name for the new worktree. When omitted,
10 pick one (step 1 below).
11- **repo** — optional path; create the worktree in this repo instead of the
12 session's current one.
13- **task** — optional; what to do inside the new worktree. No task means enter
14 the worktree and wait.
15
16Tokens before the `--` are the branch and/or repo: a path-shaped token
17(starting with `/`, `~`, `./`, or `../`) is the repo; any other token is the
18branch (`docs` is a branch name, never the `docs/` directory). More than one
19branch-shaped token before a `--` doesn't fit the grammar — ask. Without a
20`--`, judge where the task starts: leading tokens that read as a branch name
21(`fix-auth`) or a repo path are consumed as such, and the rest is the task;
22otherwise the whole input is the task (`fix the parser bug` has no
23branch-shaped lead — all task).
24
25```
26/wt-switch-create my-feature -- fix the parser bug
27/wt-switch-create -- fix the parser bug
28/wt-switch-create my-feature ~/workspace/other-repo -- fix the parser bug
29/wt-switch-create my-feature
30```
31
32## What to do
33
34Creating the worktree comes first on every invocation, before any other work.
35The invocation is itself the explicit request to create it; a research or
36read-only task gets one all the same.
37
38<!-- Maintainers: rationale.md (same directory) covers the harness rules and
39design choices behind this — read it before re-adding guards or routes. -->
40
411. **Pick the branch name** if none was given: short, from the task and
42 consistent with existing worktree names, or, mid-session, from the work
43 being moved; with nothing to derive from, ask.
44
452. **With no repo argument, create and enter in one call:**
46 `EnterWorktree({name: "<branch>"})`. Worktrunk's `WorktreeCreate` hook runs
47 `wt switch --create`, so the result is an ordinary `wt` worktree in the
48 default layout, and the user sees no confirmation prompt. On success,
49 do the task (or, with no task text, confirm it's ready and wait).
50
51 Mid-session, carry uncommitted work across: `git stash push -u` before the
52 `EnterWorktree` call, then `git stash pop` after — the call re-roots the
53 session into the new worktree, and the stash is shared across worktrees.
54
553. **Otherwise create it with `wt` and enter by path.** Two cases reach here: a
56 repo argument, which step 2 can't target, and a failed step 2, whose error
57 says which — `✗ Branch <branch> already exists`, or `Already in a worktree
58 session`. Create with a `Bash` call (omit `-C <repo>` for this repo):
59
60 ```
61 wt -C <repo> switch --create <branch> --no-cd --format=json
62 ```
63
64 Stdout is JSON whose `path` field is the worktree's absolute path (status
65 lines go to stderr). On `Branch <branch> already exists`: if the user named
66 the branch, rerun without `--create` (it enters the branch, creating its
67 worktree if missing); if step 1 picked the name, pick another and rerun. Any
68 other failure (not a git repo, invalid name): report it and stop.
69
70 Then call `EnterWorktree({path: "<path from the JSON>"})`.
71
72 - **Accepted** → the session is re-rooted in the worktree. Do the task (or,
73 with no task text, confirm it's ready and wait).
74 - **Tool error** — the tool ran and returned an error (`Cannot enter
75 worktree: …`) → graceful; nothing moved, and one recovery covers them
76 all. Common causes: the cwd resolves to no git repo (e.g. a non-git
77 parent like `~/workspace` that only holds repos, as in a background job)
78 or to a different repo than the target; or the session is already rooted
79 in a worktree (or is a pinned agent), which limits entry to the current
80 repo's `.claude/worktrees/` and excludes even a same-repo `wt` sibling.
81 The recovery test is whether you can `cd` into the worktree, which works
82 when it's inside an allowed directory. So `cd <path>` and read the
83 result:
84 - no `Shell cwd was reset` notice → it stuck; the worktree is reachable.
85 Work there, but a bare `cd` is not a tracked re-root, so the cwd can
86 revert to the session's launch worktree across turns (and in spawned
87 subagents); pin commands with `git -C <path>` / `wt -C <path>` rather
88 than trusting the `cd` to persist.
89 - `Shell cwd was reset` → not reachable. Stop and ask the user to make it
90 reachable: add the repo, or a parent like `~/workspace`, to
91 `permissions.additionalDirectories` (durable, every session), or run
92 `/add-dir <path>` (this session). Then continue. Don't grind through
93 absolute paths with `cd` resetting on every command.
94 - **Denied** — the call itself was refused, with no tool error → however
95 the denial is worded, it is the user's answer to the confirmation Claude
96 Code shows for entering a worktree outside `.claude/worktrees/`, unless
97 there was no user to ask (the denial says the session couldn't prompt),
98 which decides nothing — take the recovery above. On the user's answer:
99 the worktree `wt` just created still exists; only the entry didn't
100 happen. Report its path and ask how to proceed, since reaching it
101 through `cd` would override that answer.
102
103## Cleanup
104
105The worktree is a normal worktrunk worktree: it shows up in `wt list` and is
106merged or removed with `wt merge` / `wt remove <branch>` like any other. Don't
107remove it unprompted.
108
109A worktree from step 2 that the session never touched — no changed files, no
110commits — is cleaned up when the session ends, branch included; anything
111written into it keeps it. A worktree from step 3 always stays. If the user asks
112to leave mid-session, `ExitWorktree({action: "keep"})` returns the session to
113its original directory;
114`ExitWorktree` cannot remove a worktree entered by `path`, so removing one of
115those is always `wt remove <branch>`.
116
117## Scope
118
119The command's mandate is ONE worktree (in the named repo, if one was given)
120and the requested task inside it. Commits, pushes, and merges still each
121require explicit user permission.