Create a new git worktree and branch.
Usage
/mkwt <name> — Create a worktree with a new branch <name> from the repo's default branch (e.g. main, dev).
The <name> argument is required. If not provided, ask the user for a name.
Workflow
-
Validate the name argument is provided.
-
Detect the worktree base directory by inspecting existing worktrees:
git worktree list- Look at paths of worktrees OTHER than the main worktree (the first entry, which is the repo itself).
- If secondary worktrees exist, derive the base directory from their common parent. For example, if existing worktrees are at
/Users/foo/worktrees/meadow/branch_aand/Users/foo/worktrees/meadow/branch_b, the base is/Users/foo/worktrees/meadow/. - If NO secondary worktrees exist (only the main repo), ask the user where to create the worktree.
-
Check the branch doesn't already exist:
result=$(git branch --list <name>); if [ -n "$result" ]; then echo "EXISTS"; else echo "FREE"; fiImportant:
git branch --listalways exits 0, even with no matches. You must check the output (non-empty = exists), not the exit code. If it exists, tell the user and stop. -
Check the worktree path doesn't already exist:
ls <BASE_DIR>/<name>If it exists, tell the user and stop.
-
Confirm with the user before creating:
Create worktree
<name>at<BASE_DIR>/<name>(new branch from<DEFAULT_BRANCH>)? -
Create the worktree:
git worktree add <BASE_DIR>/<name> -b <name>Important: This command writes outside the project directory, so it requires
dangerouslyDisableSandbox: true. If it fails due to sandbox restrictions, the branch may still be created (partial failure). Delete the branch withgit branch -d <name>before retrying. -
Print the result:
Created worktree
<name>at<BASE_DIR>/<name>.