Worktree Isolation Skill
What you do
- Read an existing plan (
plan.md, PRD issue, or user-provided description) to understand the implementation scope. - Discover the workspace's conventions for commits, branching, build tooling, and testing.
- Create a git worktree on a new feature branch, isolated from the current working tree.
- Implement the plan entirely within the worktree, leaving the main working tree clean.
- When done, summarize the changes, providing a clear overview of what was implemented.
- Clean up the worktree on user request after the branch is merged.
When to use
Activate when the user asks to:
- implement a PRD, plan, or spec in an isolated worktree,
- keep implementation changes separate from ongoing work in the main checkout,
- work on a feature branch without disturbing the current working tree, or
- mentions "worktree" alongside any implementation or plan-execution task.
Non-goals
- Do not begin implementing without a clear, confirmed plan or scope.
- Do not merge or push branches without explicit user instruction.
- Do not delete the worktree without user confirmation.
Convention hierarchy
Before doing anything else, understand the workspace conventions. Apply them in this order — higher priority overrides lower:
- Explicit workspace config — checked in files like
commitlint.config.js,.commitlintrc,.husky/,.github/copilot-instructions.md,AGENTS.md,CONTRIBUTING.md,package.jsonscripts. - Inferred repo patterns — inspect recent commit messages, branch names, and existing PR titles to infer what format is in use.
- Sensible engineering defaults — when nothing is configured, apply: Conventional Commits format (
type: description),mainas the base branch, and the workspace's own package manager and test commands.
Never invent workspace-specific constraints (ticket ID formats, scopes, base branch names) that aren't evidenced by the workspace itself.
Procedure
1. Locate and confirm the plan
Look for the implementation plan in this order:
- A file the user explicitly references (e.g.
@plan.md,@prd.md, a GitHub issue number). - The conversation history if the user described the work inline.
- A
plan.mdin the session state folder (if the user mentions one exists). - A file in ../plans relative to the repo root
Summarize the scope and confirm with the user before proceeding:
- What will be implemented?
- Which areas of the codebase are affected?
2. Discover workspace conventions
Discover each of the following and record the findings; they drive all later steps: base branch, commit message format, branch naming, package manager and install command, build/test/lint commands, and whether a ticket ID is required. The discovery commands and the package-manager table are in references/convention-discovery.md.
Only require a ticket ID if the commitlint config enforces it.
3. Determine branch name and worktree path
Derive a branch name following the workspace's observed pattern:
- With ticket (if required):
feat/<ticket-id>-<short-description> - Without ticket:
feat/<short-description>
Determine the worktree path — sibling of the repo root by default:
<repo-parent>/<repo-name>-<short-description>
Example: repo at ~/dev/myorg/myrepo → worktree at ~/dev/myorg/myrepo-add-payments
Confirm both with the user before creating anything.
4. Note any uncommitted work in the current checkout
git status --short
git worktree add does not require a clean working tree. Note any uncommitted changes to the user so they are aware those changes remain in the main checkout and are not carried into the new worktree.
5. Preflight: check for collisions
# Branch already exists locally?
git branch --list <branch-name>
# Branch already checked out in another worktree?
git worktree list
# Worktree path already exists?
ls <worktree-path> 2>/dev/null && echo "PATH EXISTS"
If a collision is found:
- Branch exists: Ask whether to reuse it (
git worktree add <path> <branch-name>) or choose a new name. - Branch checked out elsewhere: A branch can only be active in one worktree at a time. The user must remove the other worktree or use a different branch.
- Path exists and is non-empty: Ask for a different path or confirm deletion.
6. Fetch the base branch
git fetch origin
7. Create the worktree
git worktree add -b <branch-name> <worktree-path> origin/<base-branch>
git worktree list
8. Bootstrap the worktree environment
Run the install/setup command discovered in step 2, from the worktree root:
cd <worktree-path> && <install-command>
For npm workspaces, always install from the workspace root — never in a subfolder, as this can corrupt the lockfile and workspace links.
9. Switch working directory into the worktree
The worktree path is a sibling of the repo root and may be outside Copilot's allowed directories. If file operations fail, first run:
/add-dir <worktree-path>
Then switch Copilot's working directory:
/cwd <worktree-path>
Alternatively, prefix bash commands with cd <worktree-path> && and use absolute paths for file edits if /cwd is not available or practical.
10. Implement the plan
Follow the plan step by step, using the SQL todo tracker or plan.md task list:
- Mark each todo
in_progressbefore starting,doneon completion. - Keep commits small and scoped to a single concern.
- Format commit messages according to the conventions discovered in step 2.
Validate after each logical unit of work using the build/test commands discovered in step 2. Where Nx is present, prefer affected-only runs over full repo runs:
npx nx affected -t compile --base=origin/<base-branch> --output-style=stream
npx nx affected -t test --base=origin/<base-branch> --output-style=stream
npx nx affected -t lint --base=origin/<base-branch> --output-style=stream
For non-Nx workspaces, use the relevant test, build, and lint scripts from package.json.
11. Run final quality gates
Run the workspace's standard quality gates before hand-off. Use affected/scoped commands where available — full repo-wide runs only if the workspace requires them or the user requests it.
All gates must pass with zero errors.
12. Summarize and hand off
Provide the user with:
- A summary of all changes (files modified, tests added, migrations, schema changes).
- The branch name and worktree path.
- Commands to push:
cd <worktree-path>
git push -u origin <branch-name>
13. Worktree cleanup (on user request only)
After the branch is merged:
git worktree remove <worktree-path>
git branch -d <branch-name>
git worktree prune
To remove before merging (destructive):
git worktree remove --force <worktree-path>
git branch -D <branch-name>
Key commands reference
A condensed list of worktree commands (preflight, create, reuse, bootstrap, cleanup) is in references/commands.md.
General notes
- The worktree shares
.githistory with the main repo — no history duplication; branches are immediately visible in the main checkout. git worktree adddoes not require a clean working tree in the main checkout.affectedcommands (Nx, Turborepo) work correctly in a worktree because they diff against the git base — not via a shared build cache (cache reuse is not guaranteed).- Running multiple dev servers from different worktrees simultaneously may cause port conflicts — check service port configuration before doing so.
- Shared infrastructure (databases, message queues, secrets) is shared across worktrees — coordinate with your team before running destructive operations like database migrations.
- Any schema or event contract changes that affect other services should be flagged to the user before committing.
Exit criteria
- All todos from the plan are marked
done. - All relevant tests pass.
- Lint and format checks pass.
- Commits follow the workspace's commit message convention.
- The user has the push commands and knows the worktree path.