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
7/24/2026
Claude CodeCursorWindsurf
#git#worktree#cleanup#branching

Recommended for

Automated summary

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.
How this analysis is made

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.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/automatic-worktree-cleanup-mryiqgdw' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/automatic-worktree-cleanup-mryiqgdw/download' --output '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md.part' && mv '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md.part' '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

Tools declared by the author (check availability in your agent): Bash(git:*), Bash(cd:*), Bash(pwd:*), Bash(bash:*)

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

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md' && test -s '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/worktree-cleanup' && cp -R '.skill-downloads/automatic-worktree-cleanup-mryiqgdw' '.claude/skills/worktree-cleanup'

Cursor
test -f '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md' && test -s '.skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/worktree-cleanup' && cp -R '.skill-downloads/automatic-worktree-cleanup-mryiqgdw' '.cursor/skills/worktree-cleanup'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/automatic-worktree-cleanup-mryiqgdw'

openskills read 'automatic-worktree-cleanup-mryiqgdw'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/automatic-worktree-cleanup-mryiqgdw/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills