Agent skill
worktree-cleanup
Remove a git worktree and its branch after PACT workflow completes. Use when: PR is merged, sub-scope work is consolidated, or manual cleanup is needed. Triggers on: worktree cleanup, remove worktree, delete worktree, branch cleanup.
Install this agent skill to your Project
npx add-skill https://github.com/Synaptic-Labs-AI/PACT-Plugin/tree/main/pact-plugin/skills/worktree-cleanup
SKILL.md
Worktree Cleanup
Remove a git worktree and its associated branch after work is complete. Typically invoked after a PR is merged, after CONSOLIDATE merges sub-scope branches, or manually by the user.
When to Use
- After
peer-reviewmerges a PR (automatic cleanup) - After CONSOLIDATE merges sub-scope branches
- Manual cleanup of stale worktrees (
/PACT:worktree-cleanup) - User aborts a workflow and wants to clean up
Process
Follow these steps in order. Surface all git errors clearly — the user resolves them.
Step 1: Identify Target
Determine which worktree to remove.
If a worktree path or branch name was provided: Use that directly.
If no target was specified: List all worktrees and ask the user which to clean up.
git worktree list
Present the list and ask: "Which worktree should I remove?"
Step 2: Navigate to Repo Root and Remove the Worktree
Before removal, the shell's working directory must NOT be inside the worktree being removed. Compute the repo root, cd to it, and remove the worktree — all in a single bash call. This is critical because if the shell CWD is inside the deleted worktree, all subsequent commands will fail.
# Compute repo root, cd there, then remove the worktree — must be ONE bash call
MAIN_GIT_DIR=$(git rev-parse --git-common-dir)
REPO_ROOT=$(cd "$(dirname "$MAIN_GIT_DIR")" && pwd)
cd "$REPO_ROOT" && git worktree remove "$REPO_ROOT/.worktrees/{branch}"
Note: Claude Code's Bash tool persists the working directory between calls. After this command, subsequent calls will run from $REPO_ROOT.
If removal fails (uncommitted changes):
Git will refuse with an error like: fatal: cannot remove: '.worktrees/{branch}' has changes.
Surface this to the user:
Cannot remove worktree — uncommitted changes exist in .worktrees/{branch}.
Options:
1. Commit or stash changes first, then retry cleanup
2. Force removal: git worktree remove --force "$REPO_ROOT/.worktrees/{branch}"
(This discards uncommitted changes permanently)
Do NOT force-remove automatically. The user must choose.
Step 3: Clean Up Paused State
Remove any paused-state.json left from a previous /PACT:pause invocation. The slug is the project directory basename (same as session_init.py's derivation).
rm -f ~/.claude/pact-sessions/{slug}/paused-state.json
This is best-effort — skip silently if the file or directory doesn't exist.
Step 4: Delete the Branch
After the worktree is removed, delete the local branch.
git branch -d {branch}
If deletion fails (branch not fully merged):
Git will refuse with an error like: error: branch '{branch}' is not fully merged.
Surface this to the user:
Cannot delete branch — '{branch}' is not fully merged.
Options:
1. Merge the branch first, then retry cleanup
2. Force delete: git branch -D {branch}
(This deletes the branch even if unmerged — changes may be lost)
Do NOT force-delete automatically. The user must choose.
Step 5: Report
Cleaned up worktree for {branch}
Worktree removed: .worktrees/{branch}
Branch deleted: {branch}
Edge Cases
| Case | Handling |
|---|---|
| Worktree has uncommitted changes | Surface git error, offer commit/stash or force options |
| Branch not fully merged | Surface git error, offer merge or force-delete options |
| Worktree directory already gone | Run git worktree prune to clean up stale refs, then delete branch |
| Currently inside the target worktree | Navigate to main repo root before removal |
| No worktrees exist | Report "No worktrees found" |
| Multiple worktrees for related branches | List all, let user choose which to remove |
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
n8n-code-javascript
Write JavaScript code in n8n Code nodes. Use when writing JavaScript in n8n, using $input/$json/$node syntax, making HTTP requests with $helpers, working with dates using DateTime, troubleshooting Code node errors, or choosing between Code node modes.
n8n-node-configuration
Operation-aware node configuration guidance. Use when configuring nodes, understanding property dependencies, determining required fields, choosing between get_node detail levels, or learning common configuration patterns by node type.
pact-coding-standards
Clean code principles, error handling patterns, and coding standards for PACT Code phase. Use when: implementing features, refactoring code, reviewing code quality, establishing coding conventions, or handling errors and exceptions. Triggers on: code quality, clean code, refactoring, error handling, logging patterns, naming conventions, code review, code phase.
pact-handoff-harvest
HANDOFF discovery, review, save, and cleanup workflow for the PACT secretary. Use when: processing agent HANDOFFs after workflow phases, running session consolidation, or recovering orphaned breadcrumbs from prior sessions. Triggers: harvest HANDOFFs, process HANDOFFs, incremental, consolidation, breadcrumb recovery.
n8n-code-python
Write Python code in n8n Code nodes. Use when writing Python in n8n, using _input/_json/_node syntax, working with standard library, or need to understand Python limitations in n8n Code nodes.
n8n-workflow-patterns
Proven workflow architectural patterns from real n8n workflows. Use when building new workflows, designing workflow structure, choosing workflow patterns, planning workflow architecture, or asking about webhook processing, HTTP API integration, database operations, AI agent workflows, or scheduled tasks.
Didn't find tool you were looking for?