Overview
Ensure work happens in an isolated workspace. Prefer your platform's native worktree tooling. Only fall back to manual git worktree when no native tooling is available.
Core principle: Detect existing isolation first. Then use native tooling. Then fall back to git. Never fight the harness.
Announce at the start: "I'm using the using-git-worktrees skill to set up an isolated workspace."
Step 0: Detect existing isolation
Before creating anything, check whether you're already in an isolated workspace.
GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
BRANCH=$(git branch --show-current)
Submodule guard: Inside a git submodule GIT_DIR != GIT_COMMON is also true. Before concluding "already inside a worktree", first confirm you're not in a submodule:
# 如果这条命令返回路径,说明你在 submodule 里,不是 worktree —— 按普通仓库处理
git rev-parse --show-superproject-working-tree 2>/dev/null
If GIT_DIR != GIT_COMMON(and not a submodule): You're already inside a linked worktree. Skip to step 2 (project setup).Do notcreate another worktree.
Report by branch state:
- On a branch: "Already in an isolated workspace
<path>, branch<name>。" - Detached HEAD: "Already in an isolated workspace
<path>(detached HEAD, externally managed). A branch will need to be created on completion."
If GIT_DIR == GIT_COMMON(or inside a submodule): You're in a normal repository checkout.
Has the user already stated a worktree preference in your instructions? If not, ask for consent before creating a worktree:
"Would you like me to set up an isolated worktree? It protects your current branch from changes."
If the user has already stated a preference, follow it directly and don't ask again. If the user declines consent, work in place and skip to step 2.
Step 1: Create an isolated workspace
You have two mechanisms. Try them in this order.
1a. Native Worktree tooling (preferred)
The user has already requested an isolated workspace (consent obtained in step 0). Do you already have a way to create a worktree? It might be a tool named EnterWorktree、WorktreeCreate , a/worktree command, or a --worktree flag. If so, use it, then skip to step 2.
Native tooling automatically handles directory placement, branch creation, and cleanup. Using git worktree addwhen you already have native tooling creates "phantom state" that your harness can't see or manage.
Only proceed to step 1b when no native worktree tooling is available.
1b. Git Worktree fallback
Use only when step 1a doesn't apply — you have no native worktree tooling available. Create the worktree manually with git.
Directory selection
Follow this priority order. An explicit user preference always takes precedence over observed filesystem state.
-
Check whether a worktree directory preference is declared in your instructions. If the user has specified one, use it directly without asking.
-
Check whether a project-local worktree directory exists:
ls -d .worktrees 2>/dev/null # 首选(隐藏目录) ls -d worktrees 2>/dev/null # 备选If found, use it. If both exist,
.worktreespriority. -
If there is no other information to reference, by default use the
.worktrees/。
Security verification (project-local directory only)
Before creating a worktree, you must verify that the directory is ignored:
git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null
If it is not ignored: Add it to .gitignore, commit that change, then continue.
Why it matters: Prevents worktree contents from being accidentally committed to the repository.
Create the worktree
# 根据选定位置确定路径
path="$LOCATION/$BRANCH_NAME"
git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"
Sandbox fallback: If git worktree add fails due to a permission error (sandbox denial), tell the user that the sandbox blocked worktree creation and that you will work in place in the current directory. Then run setup and baseline tests in place.
Step 2: Project setup
Automatically detect and run the corresponding setup command:
# Node.js
if [ -f package.json ]; then npm install; fi
# Rust
if [ -f Cargo.toml ]; then cargo build; fi
# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi
# Go
if [ -f go.mod ]; then go mod download; fi
Step 3: Verify the baseline is clean
Run tests to ensure the workspace's initial state is clean:
# 使用项目对应的命令
npm test / cargo test / pytest / go test ./...
If the tests fail: Report the failure and ask whether to continue or investigate.
If the tests pass: Report ready.
Report
工作树已就绪:<full-path>
测试通过(<N> 个测试,0 个失败)
准备实现 <feature-name>
Quick reference
| Situation | Action |
|---|---|
| Already inside a linked worktree | Skip creation (Step 0) |
| Inside a submodule | Treat as a normal repository (Step 0 guard) |
| Has a native worktree tool | Use it (Step 1a) |
| No native tool | git worktree fallback (Step 1b) |
.worktrees/ exists |
Use it (verify it is ignored) |
worktrees/ exists |
Use it (verify it is ignored) |
| Both exist | Use .worktrees/ |
| Neither exists | Check the instructions file, then default to .worktrees/ |
| Directory is not ignored | Add to .gitignore + commit |
| Permission error during creation | Sandbox fallback, work in place |
| Baseline tests fail | Report failure + ask |
| No package.json/Cargo.toml | Skip dependency installation |
Common rationalization excuses
| Excuse | Reality |
|---|---|
| "I'm obviously not in a worktree, no need to check" | Run Step 0. Isolation and submodules created by the host environment can fool the naked eye; only the detection command can settle it. |
"git worktree add is faster than finding a native tool" |
Native tools (such as EnterWorktree) manage location, branch, and cleanup. Bypassing it is thenumber one mistake — it creates ghost state that your host environment cannot see or manage. |
| "This worktree directory must already be ignored" | Run git check-ignore. A worktree directory that is not ignored will commit the entire tree into the repository. |
| "Any directory name will do" | Explicit instructions > existing project-local directory > .worktrees/ default. |
| "The workspace is brand new, baseline tests can wait" | A dirty baseline makes every subsequent failure ambiguous. Run the tests now; continuing past failures is your human partner's decision. |