From worktree
Orchestration script for cleaning up git worktrees in devcontainer environments with VS Code workspace integration. Provides SDD ticket status checking to prevent accidental cleanup of incomplete work, with confirmation prompts and dry-run mode for safety.
npx claudepluginhub manifoldlogic/claude-code-plugins --plugin worktreeThis skill uses the workspace's default tool permissions.
**Last Updated:** 2026-02-07
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Checks Next.js compilation errors using a running Turbopack dev server after code edits. Fixes actionable issues before reporting complete. Replaces `next build`.
Last Updated: 2026-02-07
Script Source: .devcontainer/scripts/cleanup-worktree.sh
The cleanup-worktree.sh script orchestrates the complete worktree removal workflow by combining multiple operations into a single command:
Unlike basic git worktree remove or crewchief worktree clean, cleanup-worktree.sh provides safety features including ticket status checking and confirmation prompts to prevent accidental data loss.
KEY SAFETY FEATURES:
For Container Execution (primary mode):
/workspace/repos directoryOptional:
Check prerequisites before running cleanup-worktree.sh:
# Verify execution context
uname # Should show Linux in container
# Check CrewChief CLI
which crewchief
# Check jq
which jq
# Verify workspace-folder.sh script
ls ~/.devcontainer/scripts/workspace-folder.sh
# Check SDD plugin (optional)
echo $SDD_ROOT_DIR
ls $SDD_ROOT_DIR/tickets/ 2>/dev/null
cleanup-worktree.sh <worktree-name> --repo <repository> [OPTIONS]
worktree-name - Name of the worktree to remove (alphanumeric, hyphens, underscores only)-r, --repo REPO - Repository name (must exist in /workspace/repos)-w, --workspace FILE - VS Code workspace file path (default: from WORKSPACE_FILE or auto-detect)-y, --yes - Skip confirmation prompt (for scripted usage)--keep-branch - Don't delete the git branch after worktree removal--skip-workspace - Skip removing from VS Code workspace--dry-run - Show what would be done without making changes-h, --help - Show help message and exitAll environment variables can be overridden by CLI flags (flags take precedence):
WORKSPACE_FILE - Path to VS Code workspace file
/workspace/workspace.code-workspace-w/--workspace flagSDD_ROOT_DIR - Path to SDD directory for ticket detection
When SDD_ROOT_DIR is configured, the cleanup script automatically:
| Status | Meaning | Behavior |
|---|---|---|
| complete | All tasks verified | Cleanup proceeds without extra prompts |
| partial | Some tasks verified | Warning shown, confirmation required |
| not_started | No tasks verified | Warning shown, confirmation required |
| no_tasks | Ticket exists but no tasks | Cleanup proceeds (empty ticket) |
| no_ticket | No associated ticket found | Cleanup proceeds normally |
The script searches for tickets in this order:
cleanup-worktree.sh feature-auth --repo myproject
Output (with complete ticket):
[INFO] Checking for associated SDD ticket...
[INFO] Found ticket: FEATURE_auth-system (complete)
[INFO] All tasks verified - no confirmation needed
[INFO] Removing worktree 'feature-auth' from repository 'myproject'...
[OK] Worktree removed
[INFO] Deleting branch 'feature-auth'...
[OK] Branch deleted
[INFO] Removing from VS Code workspace...
[OK] Workspace updated
Use case: Standard workflow after completing and merging feature work. Ticket is complete, so no confirmation needed.
cleanup-worktree.sh bugfix-login --repo myproject
Output (with partial ticket):
[INFO] Checking for associated SDD ticket...
[WARN] Found ticket: BUGFIX_login-issue
Status: partial (2/5 tasks verified)
Location: /workspace/_SDD/tickets/BUGFIX_login-issue
This worktree has incomplete work. Do you want to proceed? [y/N]:
Use case: Attempting to clean up work that isn't finished. The confirmation prompt prevents accidental data loss.
cleanup-worktree.sh experiment --repo myproject --keep-branch
Output:
[INFO] Removing worktree 'experiment' from repository 'myproject'...
[OK] Worktree removed
[INFO] Keep branch: yes (--keep-branch specified)
[INFO] Removing from VS Code workspace...
[OK] Workspace updated
Use case: Clean up worktree but preserve the branch for later. Useful when you want to free up disk space but might return to the work.
cleanup-worktree.sh test-feature --repo myproject --yes
Output:
[INFO] Skipping ticket check (--yes specified)
[INFO] Removing worktree 'test-feature'...
[OK] Worktree removed
Use case: Automated scripts or when you've already verified the work is complete. Use with caution.
cleanup-worktree.sh feature-test --repo myproject --dry-run
Output:
==========================================
DRY RUN - No changes will be made
==========================================
Would execute with resolved parameters:
Repository: myproject
Worktree name: feature-test
Keep branch: no
Skip workspace: no
Workspace file: /workspace/workspace.code-workspace
Ticket check:
Found: FEATURE_test-feature
Status: complete
Action: Would proceed without confirmation
Commands that would run:
1. Remove worktree:
crewchief worktree clean feature-test --repo myproject
Expected path: /workspace/repos/myproject/feature-test
2. Delete branch:
git -C /workspace/repos/myproject/myproject fetch --prune
git -C /workspace/repos/myproject/myproject branch -d feature-test
3. Update workspace:
workspace-folder.sh remove repos/myproject/feature-test
Use case: Preview all actions before executing. Verify ticket detection and workspace resolution are correct.
cleanup-worktree.sh hotfix --repo myproject --skip-workspace
Output:
[INFO] Removing worktree 'hotfix'...
[OK] Worktree removed
[INFO] Workspace: Skipped (--skip-workspace)
Use case: When you've already removed the folder from workspace manually, or don't use VS Code workspaces.
The script uses specific exit codes to indicate different outcomes:
| Exit Code | Meaning | Common Causes | Solution |
|---|---|---|---|
| 0 | Success | Worktree removed successfully | (Success - no action needed) |
| 1 | Docker or container issues | Container not running | Start container |
| 2 | Missing prerequisites | crewchief CLI or jq not installed | Install required tools |
| 3 | Invalid arguments | Missing required args, invalid format | Check syntax: --help |
| 4 | Worktree not found | Worktree doesn't exist, already removed | Verify worktree name |
| 5 | User cancelled | User answered 'n' to confirmation | Re-run if intended |
[ERROR] crewchief CLI is required for worktree operations
[ERROR] Install from: https://github.com/your-org/crewchief
Solution: Install CrewChief CLI in the devcontainer.
[ERROR] Worktree 'feature-xyz' not found in repository 'myproject'
[ERROR] Available worktrees:
- main
- feature-auth
- bugfix-123
Solution: Check the worktree name and verify it exists:
crewchief worktree list --repo myproject
[ERROR] Cannot remove worktree - you are currently in it
[ERROR] Switch to a different worktree first:
cd /workspace/repos/myproject/main
Solution: Navigate to a different worktree before cleanup:
cd /workspace/repos/myproject/main
cleanup-worktree.sh feature-old --repo myproject
[WARN] Workspace file not found: /workspace/workspace.code-workspace
[WARN] Skipping workspace update
Solution: This is non-fatal. Specify workspace file with -w:
cleanup-worktree.sh feature-old --repo myproject -w /path/to/workspace.code-workspace
[ERROR] jq is required for workspace updates
[ERROR] Install with: apt-get install jq
[ERROR] Or skip workspace updates with: --skip-workspace
Solution: Install jq or skip workspace updates:
sudo apt-get install jq
# OR
cleanup-worktree.sh feature --repo myproject --skip-workspace
[WARN] Branch 'feature-xyz' has unmerged commits
[WARN] Use --keep-branch to preserve the branch
Solution: Either merge the work first, or use --keep-branch:
cleanup-worktree.sh feature-xyz --repo myproject --keep-branch
For worktree creation with workspace integration, see the worktree-spawn skill.