Automatic Worktree Cleanup

VerifiedCaution

Automatically cleans up Git worktrees and returns to the base branch. Keeps local and remote branches intact.

Sby Skills Guide Bot
DevelopmentIntermediate
107/24/2026
Claude CodeCursorWindsurf
#git#worktree#cleanup#branching

Recommended for

Our review

Automates the cleanup of a git worktree by detecting the current branch, checking for uncommitted changes, removing the worktree, and checking out the base branch.

Strengths

  • Saves time by automating multi-step cleanup process
  • Prevents accidental deletion of local and remote branches
  • Handles common error cases like uncommitted changes with clear feedback
  • Allows specifying a base branch and forcing execution when needed

Limitations

  • Only works when run from within a worktree directory
  • Does not delete local or remote branches
  • Requires all changes to be committed by default
When to use it

Use after completing work in a worktree and before switching branches to maintain a clean development environment.

When not to use it

Do not use if you intend to keep the worktree directory for future work, or if you have uncommitted changes and cannot force.

Security analysis

Caution
Quality score90/100

The skill uses Bash with extensive permissions to run a cleanup script that performs git operations (remove worktree, checkout base branch). While the intended actions are safe and non-destructive, the broad Bash access poses a moderate risk if the script were to be modified or misused. No destructive or exfiltrating instructions are directly included.

Findings
  • Broad Bash access (Bash(bash:*)) could permit execution of arbitrary commands if the cleanup script is compromised.

Examples

Clean up current worktree
I'm done with this worktree, please clean it up and switch back to main.
Clean up to develop branch
Clean up the current worktree and checkout the develop branch.
Force cleanup with uncommitted changes
Force clean up this worktree even if there are uncommitted changes, but I want to keep the branch.

name: worktree-cleanup description: Automatically cleanup the worktree and return to the base branch. Keeps local and remote branches intact. Check after creating PR or pushing. allowed-tools: Bash(git:), Bash(cd:), Bash(pwd:), Bash(bash:)

Worktree Cleanup

Automatically cleans up the worktree after development is complete.

Overview

This skill performs the following steps automatically:

  1. Detects the current worktree directory and branch
  2. Checks for uncommitted changes
  3. Removes the worktree
  4. Checks out the base branch (default: main)

Important: Local and remote branches are preserved (not deleted).

Usage

Basic Usage

# Run from within the worktree directory
bash scripts/worktree_cleanup.sh

Options

# Specify a base branch
bash scripts/worktree_cleanup.sh --base develop

# Skip confirmation prompt
bash scripts/worktree_cleanup.sh --yes

# Ignore uncommitted changes (not recommended)
bash scripts/worktree_cleanup.sh --force

Prerequisites

  • Must be run from within a worktree directory
  • All changes must be committed (or use the --force option)

Error Handling

Uncommitted Changes Detected

The script will stop and prompt you to commit. You can force execution with --force, but this is not recommended.

Worktree Removal Failed

Manual removal instructions will be displayed.

Running Outside a Worktree

A warning is displayed and the process is aborted.

Details

See REFERENCE.md for internal processing flow, troubleshooting, and best practices.

Related skills