HE Worktree
Prepare safe workspace isolation before running implementation-heavy phases.
When to Use
- Before
he-spikewhen creating throwaway prototypes - Before
he-implementfor non-trivial or long-running work - Before broad review/refactor passes that may touch many files
- Any time the current branch/workspace ownership is unclear
When NOT to Use
- Bootstrap scaffolding (
he-bootstrap) — docs structure commits directly to main - Initial project scaffolding — generating the app skeleton (e.g.,
bun create next-app,cargo init,rails new) belongs on main as the baseline. Feature branches start after the foundation exists. - Spec and plan creation (
he-spec,he-plan) — these are documentation artifacts that belong on main until implementation begins - Trivial doc-only changes that don't warrant isolation
Key Principles
- Isolation first — avoid collisions with human in-flight changes for non-trivial work.
- Never work on default branch without explicit consent — always ask first.
- No destructive git — do not reset/clean/delete automatically.
- Name and verify — branch/worktree naming plus explicit status verification.
- Hand off a concrete workspace — strategy, branch name, directory path.
- Runbooks are additive only — apply any runbook whose frontmatter
called_frommatches this skill (bash scripts/runbooks/select-runbooks.sh --skill he-worktree), but never waive/override anything codified here.
Workflow
Phase 0: Detect Current State
- Ensure repository context:
git rev-parse --show-toplevel - Read current branch:
git branch --show-current - Read default branch:
git remote show origin | sed -n '/HEAD branch/s/.*: //p' - Detect whether already in a worktree:
git rev-parse --git-dircontains/worktrees/ - Refresh refs:
git fetch --all --prune - Accept optional initiative slug/topic/branch hint.
- Run
bash scripts/runbooks/select-runbooks.sh --skill he-worktreeand read any returned runbooks. Apply their additions throughout — they must not waive or override gates codified here.
Phase 1: Choose Strategy
- If already in a worktree for the same initiative, continue there.
- If on the default branch, prefer creating a new worktree.
- If on a feature branch, decide between continuing that branch or creating a new worktree.
- Continuing directly on default branch requires explicit user consent.
When interactive tools are available, ask the user to choose:
- Create a new worktree (recommended)
- Create a branch in current directory
- Continue in current directory
Phase 2: Create Workspace
Branch naming — choose from context:
spike/<topic>for feasibility investigationsfeat/<topic>for feature workfix/<topic>for bug fixeschore/<topic>for maintenance
When a slug exists, prefer embedding the topic part from YYYY-MM-DD-<type>-<description>.
Worktree path:
- Derive branch name from initiative context.
- Derive directory path:
../<repo-name>-<branch-name> - Create and switch:
git worktree add ../<repo-name>-<branch-name> -b <branch-name> - If needed, copy local runtime files (
.env, local config overrides) that should not be committed. - Validate:
git -C ../<repo-name>-<branch-name> status --short --branch
Branch-only path (when worktree is unnecessary):
- Create branch:
git checkout -b <branch-name> - Verify:
git status --short --branch
Safety Rules
- Never run destructive git commands (
reset --hard,clean -fd, branch deletion) as part of this skill. - Never delete existing worktrees automatically.
- If a target worktree path already exists, inspect and ask before reusing.
- Report exact branch and directory before handing off.
Output
Return:
- Selected strategy (
worktreeorbranch) - Branch name
- Workspace directory path
- Verification command + result summary
Exit Gate
- Workspace is isolated and verified
- Branch name and directory path are explicit
- Strategy is documented for downstream phases
When Things Go Wrong
- Target worktree path already exists — inspect the existing worktree and ask the user before reusing or choosing a different path.
- Branch name conflicts with existing branch — check if the existing branch is related; if so, offer to continue on it; otherwise choose a different name.
git fetchfails — check network/remote config; do not proceed with stale refs.- User is on default branch with uncommitted work — warn explicitly; never silently overwrite in-flight changes.
Anti-Patterns to Avoid
| Anti-Pattern | Better Approach |
|---|---|
| Working directly on main/default without consent | Always isolate or get explicit approval |
| Deleting worktrees to resolve conflicts | Inspect first, ask the user |
| Skipping verification after creation | Always run git status to confirm isolation |
| Hardcoding branch prefixes without context | Match prefix to initiative type (spike/feat/fix/chore) |
Transition Points
Proceed to he-spike or he-implement in the selected isolated workspace.
