Interactively select and remove a git worktree
Interactively selects and removes a specific git worktree, handling both safe cleanup and potentially unfinished work with user confirmation.
/plugin marketplace add gopherguides/gopher-ai/plugin install go-workflow@gopher-aihaikuThis command interactively removes a single git worktree. Unlike /prune-worktree which auto-removes all safe worktrees, this command lets you select a specific worktree and handles cases where the issue isn't closed or branch isn't merged.
Usage: /remove-worktree (no arguments - interactive selection)
Clear any active worktree state so the pre-tool-use hook doesn't block cleanup commands:
!"${CLAUDE_PLUGIN_ROOT}/scripts/worktree-state.sh" clear 2>/dev/null || true
basename \git rev-parse --show-toplevel 2>/dev/null` 2>/dev/null || echo "unknown"`git remote show origin 2>/dev/null | grep 'HEAD branch' | sed 's/.*: //' || echo "main"git worktree list 2>&1 || echo "No worktrees found"git worktree list 2>/dev/null | grep -E "issue-[0-9]+" || echo "No issue worktrees found"CRITICAL: When executing bash commands below, use backticks (`) for command substitution, NOT $(). Claude Code has a bug that mangles $() syntax into broken commands. Copy the commands exactly as written.
!REPO_NAME=basename \git rev-parse --show-toplevel``
!echo "Repository: $REPO_NAME"
!echo "Scanning for issue worktrees..." !git worktree list
Filter for worktrees matching the {REPO_NAME}-issue-* pattern and display them as a numbered list.
!git worktree list | grep -E "/${REPO_NAME}-issue-[0-9]+" | cat -n
If no issue worktrees are found, inform the user and stop: "No issue worktrees found matching the pattern '{REPO_NAME}-issue-*'"
Use AskUserQuestion to ask: "Which worktree would you like to remove? Enter the number from the list above, or the full path."
Store the selected worktree path and extract:
WORKTREE_PATH: The full path to the worktreeBRANCH_NAME: The branch associated with the worktreeISSUE_NUM: The issue number extracted from the directory nameTo extract issue number from path:
!ISSUE_NUM=echo "$WORKTREE_PATH" | grep -oE 'issue-[0-9]+' | grep -oE '[0-9]+'
!echo "Issue number: $ISSUE_NUM"
To get branch name:
!BRANCH_NAME=git worktree list --porcelain | grep -A2 "worktree $WORKTREE_PATH" | grep "branch" | sed 's/branch refs\/heads\///'
!echo "Branch: $BRANCH_NAME"
!cd "$WORKTREE_PATH" && git status --porcelain
If there are uncommitted changes, warn the user immediately.
!if ! echo "$ISSUE_NUM" | grep -qE '^[0-9]+$'; then echo "Error: Could not extract valid issue number"; exit 1; fi
!gh issue view "$ISSUE_NUM" --json state,title --jq '"(.state): (.title)"'
!DEFAULT_BRANCH=git remote show origin | grep 'HEAD branch' | sed 's/.*: //' | tr -cd '[:alnum:]-._/'
!if [ -z "$DEFAULT_BRANCH" ]; then echo "Error: Could not determine default branch"; exit 1; fi
!echo "Default branch: $DEFAULT_BRANCH"
!git fetch origin "$DEFAULT_BRANCH" --quiet
!MERGED=git branch --merged "origin/$DEFAULT_BRANCH" | grep -F "$BRANCH_NAME" || echo ""
Evaluate the status:
ISSUE_CLOSED: true if GitHub issue state is "CLOSED"BRANCH_MERGED: true if branch appears in merged branchesHAS_CHANGES: true if uncommitted changes existDisplay: "This worktree is safe to remove:"
Use AskUserQuestion: "Remove this worktree?"
If confirmed: !git worktree remove "$WORKTREE_PATH" !echo "Worktree removed: $WORKTREE_PATH"
Ask: "Also delete the local branch '$BRANCH_NAME'?"
If confirmed: !git branch -d "$BRANCH_NAME"
Display a prominent warning:
⚠️ WARNING: This worktree may contain unfinished work!
Status:
- Issue #$ISSUE_NUM: [OPEN/CLOSED]
- Branch merged: [YES/NO]
- Uncommitted changes: [YES/NO]
Removing this worktree could result in PERMANENT DATA LOSS.
This action cannot be undone.
Use AskUserQuestion with a serious tone: "Are you SURE you want to force-remove this worktree? This may delete unfinished work."
Only if user explicitly confirms force removal:
!git worktree remove "$WORKTREE_PATH" --force !echo "Worktree force-removed: $WORKTREE_PATH"
Ask: "Also delete the local branch '$BRANCH_NAME'? (Use -D to force delete unmerged branch)"
If confirmed: !git branch -D "$BRANCH_NAME"
Clear the active worktree state so the pre-tool-use hook stops enforcing path prefixes:
"${CLAUDE_PLUGIN_ROOT}/scripts/worktree-state.sh" clear
Display final status: !echo "Done." !git worktree list
| Command | Use Case |
|---|---|
/prune-worktree | Batch cleanup of all completed (closed + merged) worktrees |
/remove-worktree | Remove a specific worktree, including abandoned/unfinished ones |
Use /remove-worktree when: