managing-git-worktrees
Installation
SKILL.md
Managing Git Worktrees
Inspect before mutation. Keep worktree operations separate from commits, pushes, rebases, and unrelated branch changes. Treat dynamic refs and paths as untrusted: validate them, pass them as argv when possible, and quote shell values.
Inspect
- Capture the invoking worktree, symbolic branch, and HEAD. A detached invoking HEAD requires an explicit creation start point.
- Parse
git worktree list --porcelain -z; the first record identifies the main worktree. If it is bare or stale, request a storage root or repair it before deriving paths. - Resolve the requested branch, path, and start point. Never assume the default branch name.
For a new path, default the storage root to $HOME/.worktrees; if $HOME is unset or unusable, request an explicit root. Create the root if absent, then use <root>/<main-name>-<branch-with-slashes-replaced-by-hyphens>. Stop on an existing path or normalization collision rather than inventing a suffix.
Create or Attach
- Normalize the branch with
git check-ref-format --branch "$branch"and resolve the start point to one commit withgit rev-parse --verify --end-of-options "${start_point}^{commit}". - Determine exact branch existence with
git show-ref --verifyand occupancy from porcelain records; do not substring-match. - Create or attach: