skills/git-worktree/SKILL.md
Create isolated git worktrees for clean agent sessions or delete stale ones. Creates a temporary worktree from origin/main, lets the user /move into it for isolated work, then commits, pushes a PR branch, and cleans up. Also lists and removes old worktrees on request.
npx skillsauth add paulund/ai git-worktreeInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Determine mode from the user's request:
Run at the start of either mode:
REPO_ROOT=$(git rev-parse --show-toplevel)
REPO_NAME=$(basename "$REPO_ROOT")
WORKTREE_BASE="$HOME/.local/share/opencode/worktrees/$REPO_NAME"
mkdir -p "$WORKTREE_BASE"
Ask the user:
origin/main)Generate branch name: sanitize the task description to [a-z0-9-]+, prefix with worktree/.
BRANCH="worktree/<sanitized-slug>"
TIMESTAMP=$(date +%s)
WORKTREE_PATH="$WORKTREE_BASE/$BRANCH-$TIMESTAMP"
cd "$REPO_ROOT"
git fetch origin
git worktree add -b "$BRANCH" "$WORKTREE_PATH" "$SOURCE_BRANCH"
Confirm the worktree was created successfully. If it fails (e.g. branch already exists), report and stop.
Tell the user:
Worktree created at
$WORKTREE_PATHon branch$BRANCH. Run/move $WORKTREE_PATHto switch the session context to the worktree.
Use the question tool to ask "Have you run /move? Select Yes once the session has moved."
Only proceed after they confirm. Do not proceed without confirmation.
Make the requested changes using all available tools (edit, write, bash, etc.). The session is now operating inside the worktree.
cd "$WORKTREE_PATH"
git add -A
git commit -m "<descriptive message about the work done>"
git push origin "$BRANCH"
gh pr create --fill --base main --head "$BRANCH"
Capture the PR URL from the output and show it to the user.
Tell the user:
PR created: <URL> Run
/move $REPO_ROOTto switch back to the original repo.
Use the question tool to ask "Have you run /move back? Select Yes once you're back."
Only proceed after they confirm.
cd /tmp
git worktree remove --force "$WORKTREE_PATH"
git worktree prune
Notify the user: "Worktree cleaned up."
If any step fails after the worktree is created, still ask the user to /move back and clean up. Use a trap-style pattern:
/move back if neededRun git worktree list inside the repo. Parse the output to show:
| # | Path | Branch | Age (from path timestamp if available, else from git log) | |---|------|--------|-----------------------------------------------------------|
Also list any directories in $WORKTREE_BASE that are stale.
Use the question tool to let the user select one or more worktrees to delete. Show paths clearly.
git worktree remove --force "<selected-path>"
git worktree prune
If present in $WORKTREE_BASE, also remove the stale directory:
rm -rf "$WORKTREE_BASE/<stale-dir>"
Ask: "The remote branch $BRANCH still exists on origin. Delete it too?"
If yes:
git push origin --delete "$BRANCH"
Tell the user what was cleaned up.
development
Use when implementing any logic, fixing any bug, or changing any behaviour. Use when you need to prove code works, when a bug report arrives, or when modifying existing functionality. Do NOT use for config changes, data migrations, or dependency updates.
development
Use when starting a new feature, when requirements are unclear, when asked to write code without a clear spec, or before any non-trivial implementation. Do NOT use for trivial bug fixes or one-line changes.
development
Use when you want authoritative, source-cited code free from outdated patterns. Use when building with any framework or library where correctness matters. Detects the stack from dependency files, fetches official documentation, implements following documented patterns, and cites sources for every framework-specific decision.
development
Use when preparing to ship a feature, release, or deployment. Use before merging to main, creating a release, or deploying to production. Do NOT use for CI-only changes or internal refactors that don't reach production.