/remove-worktree
Remove a specific worktree (directory + git reference + branch)
$ npx -y skills add rtk-ai/rtk --agent claude-codeHow it fires
How this command gets triggered: by you, by Claude, or both.
- Fires itselfClaude auto-loads it when your prompt matches the work.
- You can call itInvoke it directly when you want it.
- Slash command
/remove-worktree
Context preview
What this command does when you run it.
Remove a specific worktree (directory + git reference + branch)
Command definition
remove-worktree.mdmodel: haiku
description: Remove a specific worktree (directory + git reference + branch)
argument-hint: "<branch-name>"
Remove Worktree
Remove a specific worktree, cleaning up directory, git references, and optionally the branch.
Usage
/tech:remove-worktree feature/new-filter
/tech:remove-worktree fix/session-bug
Implementation
Execute this script with branch name from `$ARGUMENTS`:
#!/bin/bash
set -euo pipefail
BRANCH_NAME="$ARGUMENTS"
if [ -z "$BRANCH_NAME" ]; then
echo "β Usage: /tech:remove-worktree <branch-name>"
echo ""
echo "Example:"
echo " /tech:remove-worktree feature/new-filter"
exit 1
fi
echo "π Checking worktree: $BRANCH_NAME"
echo ""
# Check if worktree exists in git
if ! git worktree list | grep -q "$BRANCH_NAME"; then
echo "β Worktree not found: $BRANCH_NAME"
echo ""
echo "Available worktrees:"
git worktree list
exit 1
fi
# Get worktree path from git
WORKTREE_FULL_PATH=$(git worktree list | grep "$BRANCH_NAME" | awk '{print $1}')
# Safety check: never remove main repo
if [ "$WORKTREE_FULL_PATH" = "$(pwd)" ]; then
echo "β Cannot remove main repository worktree"
exit 1
fi
# Safety check: never remove master or main
if [ "$BRANCH_NAME" = "master" ] || [ "$BRANCH_NAME" = "main" ]; then
echo "β Cannot remove $BRANCH_NAME (protected branch)"
exit 1
fi
echo "π Worktree path: $WORKTREE_FULL_PATH"
echo "πΏ Branch: $BRANCH_NAME"
echo ""
# Check if branch is merged
IS_MERGED=false
if git branch --merged master | grep -q "^[* ] ${BRANCH_NAME}$"; then
IS_MERGED=true
echo "β
Branch is merged into master (safe to delete)"
else
echo "β οΈ Branch is NOT merged into master"
fi
echo ""
# Ask confirmation if not merged
if [ "$IS_MERGED" = false ]; then
echo "β οΈ This will DELETE unmerged work. Continue? [y/N]"
read -r confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "Aborted."
exit 0
fi
fi
# Remove worktree
echo "ποΈ Removing worktree..."
if git worktree remove "$WORKTREE_FULL_PATH" 2>/dev/null; then
echo "β
Worktree removed: $WORKTREE_FULL_PATH"
else
echo "β οΈ Git remove failed, forcing removal..."
rm -rf "$WORKTREE_FULL_PATH"
git worktree prune
echo "β
Worktree forcefully removed"
fi
# Delete branch
echo ""
echo "πΏ Deleting branch..."
if [ "$IS_MERGED" = true ]; then
if git branch -d "$BRANCH_NAME" 2>/dev/null; then
echo "β
Branch deleted (local): $BRANCH_NAME"
else
echo "β οΈ Local branch already deleted or not found"
fi
else
if git branch -D "$BRANCH_NAME" 2>/dev/null; then
echo "β
Branch force-deleted (local): $BRANCH_NAME"
else
echo "β οΈ Local branch already deleted or not found"
fi
fi
# Delete remote branch (if exists)
echo ""
echo "π Checking remote branch..."
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
echo "β οΈ Remote branch exists. Delete it? [y/N]"
read -r confirm_remote
if [ "$confirm_remote" = "y" ] || [ "$confirm_remote" = "Y" ]; then
if git push origin --delete "$BRANCH_NAME" --no-verify 2>/dev/null; then
echo "β
Remote branch deleted: $BRANCH_NAME"
else
echo "β Failed to delete remote branch (may require permissions)"
fi
else
echo "βοΈ Skipped remote branch deletion"
fi
else
echo "βΉοΈ No remote branch found"
fi
echo ""
echo "β
Cleanup complete!"
echo ""
echo "π Remaining worktrees:"
git worktree listSafety Features
- β
Never removes `master` or `main`
- β
Asks confirmation for unmerged branches
- β
Cleans git references, directory, and branch
- β
Optional remote branch deletion
- β
Fallback to force removal if git fails
Manual Override
git worktree remove --force <path>
git branch -D <branch>
git push origin --delete <branch> --no-verify
Read more
model: haiku description: Remove a specific worktree (directory + git reference + branch) argument-hint: "<branch-name>"
Remove Worktree
Remove a specific worktree, cleaning up directory, git references, and optionally the branch.
Usage
/tech:remove-worktree feature/new-filter /tech:remove-worktree fix/session-bug
Implementation
Execute this script with branch name from `$ARGUMENTS`:
#!/bin/bash
set -euo pipefail
BRANCH_NAME="$ARGUMENTS"
if [ -z "$BRANCH_NAME" ]; then
echo "β Usage: /tech:remove-worktree <branch-name>"
echo ""
echo "Example:"
echo " /tech:remove-worktree feature/new-filter"
exit 1
fi
echo "π Checking worktree: $BRANCH_NAME"
echo ""
# Check if worktree exists in git
if ! git worktree list | grep -q "$BRANCH_NAME"; then
echo "β Worktree not found: $BRANCH_NAME"
echo ""
echo "Available worktrees:"
git worktree list
exit 1
fi
# Get worktree path from git
WORKTREE_FULL_PATH=$(git worktree list | grep "$BRANCH_NAME" | awk '{print $1}')
# Safety check: never remove main repo
if [ "$WORKTREE_FULL_PATH" = "$(pwd)" ]; then
echo "β Cannot remove main repository worktree"
exit 1
fi
# Safety check: never remove master or main
if [ "$BRANCH_NAME" = "master" ] || [ "$BRANCH_NAME" = "main" ]; then
echo "β Cannot remove $BRANCH_NAME (protected branch)"
exit 1
fi
echo "π Worktree path: $WORKTREE_FULL_PATH"
echo "πΏ Branch: $BRANCH_NAME"
echo ""
# Check if branch is merged
IS_MERGED=false
if git branch --merged master | grep -q "^[* ] ${BRANCH_NAME}$"; then
IS_MERGED=true
echo "β
Branch is merged into master (safe to delete)"
else
echo "β οΈ Branch is NOT merged into master"
fi
echo ""
# Ask confirmation if not merged
if [ "$IS_MERGED" = false ]; then
echo "β οΈ This will DELETE unmerged work. Continue? [y/N]"
read -r confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
echo "Aborted."
exit 0
fi
fi
# Remove worktree
echo "ποΈ Removing worktree..."
if git worktree remove "$WORKTREE_FULL_PATH" 2>/dev/null; then
echo "β
Worktree removed: $WORKTREE_FULL_PATH"
else
echo "β οΈ Git remove failed, forcing removal..."
rm -rf "$WORKTREE_FULL_PATH"
git worktree prune
echo "β
Worktree forcefully removed"
fi
# Delete branch
echo ""
echo "πΏ Deleting branch..."
if [ "$IS_MERGED" = true ]; then
if git branch -d "$BRANCH_NAME" 2>/dev/null; then
echo "β
Branch deleted (local): $BRANCH_NAME"
else
echo "β οΈ Local branch already deleted or not found"
fi
else
if git branch -D "$BRANCH_NAME" 2>/dev/null; then
echo "β
Branch force-deleted (local): $BRANCH_NAME"
else
echo "β οΈ Local branch already deleted or not found"
fi
fi
# Delete remote branch (if exists)
echo ""
echo "π Checking remote branch..."
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
echo "β οΈ Remote branch exists. Delete it? [y/N]"
read -r confirm_remote
if [ "$confirm_remote" = "y" ] || [ "$confirm_remote" = "Y" ]; then
if git push origin --delete "$BRANCH_NAME" --no-verify 2>/dev/null; then
echo "β
Remote branch deleted: $BRANCH_NAME"
else
echo "β Failed to delete remote branch (may require permissions)"
fi
else
echo "βοΈ Skipped remote branch deletion"
fi
else
echo "βΉοΈ No remote branch found"
fi
echo ""
echo "β
Cleanup complete!"
echo ""
echo "π Remaining worktrees:"
git worktree listSafety Features
- β Never removes `master` or `main`
- β Asks confirmation for unmerged branches
- β Cleans git references, directory, and branch
- β Optional remote branch deletion
- β Fallback to force removal if git fails
Manual Override
git worktree remove --force <path> git branch -D <branch> git push origin --delete <branch> --no-verify
CLI proxy that reduces LLM token consumption by 60-90% on common dev commands. Single Rust binary, zero dependencies
Repo: rtk-ai/rtk
Other commands on rtk.
- /clean-worktree
Interactive cleanup of stale worktrees (merged branches, orphaned refs)
Open command - /clean-worktrees
Clean all merged worktrees automatically (no interaction)
Open command - /diagnose
RTK environment diagnostics - Checks installation, hooks, version, command routing
Open command - /audit-codebase
RTK Codebase Health Audit β 7 catΓ©gories scorΓ©es 0-10
Open command - /codereview
RTK Code Review β Review locale pre-PR avec auto-fix
Open command - /worktree-status
Check background cargo check status for a git worktree
Open command

