Skip to content
Skill

Git clean worktrees

Finds and removes git worktrees whose changes are already merged into main

Git clean worktrees is an open-source agent skill for developer tools. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

Scripts
None
Platforms
Claude Code, Codex, Cursor
GitHub stars
12
Last commit
License
MIT
Listing updated

Similar skills

About Git clean worktrees

This skill walks the agent through auditing the secondary worktrees in a git repository and removing the ones whose work has already landed on main. It checks each worktree in turn: whether its branch is reachable from main, whether a squash merge left an empty diff against main, and whether any uncommitted edits already match main's contents. Worktrees that pass one of these checks are reported as safe to remove, while those with genuinely unmerged work are flagged and left alone.

The workflow ends with a summary shown before anything is deleted, then removal of confirmed worktrees, deletion of their merged local branches, pruning of stale worktree metadata, and a final listing. Safe-delete is tried first, and force-delete is used only when the diff against main was confirmed empty. The skill never removes the main worktree and asks for confirmation before touching unmerged work.

It relies only on standard git commands run through the shell, so it needs a git repository with a branch named main. The skill ships as a SKILL.md with no bundled scripts.

When your agent uses it

Clean up stale git worktrees whose work is already on the main branch. Worktrees accumulate after branches are merged — they sit around with "modified" files that look like uncommitted work but are actually already on main. This causes confusion ("did I forget to merge this?") and clutters git status. Use this skill whenever the user mentions stale worktrees, worktree cleanup, leftover worktrees, "clean up workspaces", or when you notice orphaned worktrees during other git operations. Also trigger on: "worktrees keep getting left behind", "is this merged", "clean up worktrees", "remove old worktrees", "worktree confusion", or any mention of phantom modified files in worktree directories. Trigger proactively when `git worktree list` shows entries beyond the main worktree, especially before or after branch cleanup operations.

From the skill's SKILL.md. Your agent reads this description to decide when to load the skill.

What it can access

From a static check of its 2 files (7 KB) on GitHub on 4 December 2026. Nothing was run. Read the files before you install a skill: it can tell your agent to run anything its tools allow.

Check another skill before you install it.

Install

This skill ships in the skill-madness plugin. In Claude Code, add the marketplace and install the plugin (it may include other skills too):

/plugin marketplace add ivy00johns/Skill-Madness
/plugin install skill-madness@skill-madness

Or copy only this skill to ~/.claude/skills/git-clean-worktrees:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/ivy00johns/Skill-Madness.git "$tmp" &&
mkdir -p ~/.claude/skills &&
rm -rf ~/.claude/skills/git-clean-worktrees &&
cp -R "$tmp/skills/archive/git-clean-worktrees" ~/.claude/skills/git-clean-worktrees &&
rm -rf "$tmp"

For one project only, put the folder in .claude/skills/git-clean-worktrees inside it instead.

Claude Code docs on skills

Copy the skill folder to ~/.agents/skills/git-clean-worktrees to use it in every repository:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/ivy00johns/Skill-Madness.git "$tmp" &&
mkdir -p ~/.agents/skills &&
rm -rf ~/.agents/skills/git-clean-worktrees &&
cp -R "$tmp/skills/archive/git-clean-worktrees" ~/.agents/skills/git-clean-worktrees &&
rm -rf "$tmp"

For one repository only, put the folder in .agents/skills/git-clean-worktrees inside it instead.

Codex docs on skills

Copy the skill folder to ~/.cursor/skills/git-clean-worktrees to use it in every project:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/ivy00johns/Skill-Madness.git "$tmp" &&
mkdir -p ~/.cursor/skills &&
rm -rf ~/.cursor/skills/git-clean-worktrees &&
cp -R "$tmp/skills/archive/git-clean-worktrees" ~/.cursor/skills/git-clean-worktrees &&
rm -rf "$tmp"

For one project only, put the folder in .cursor/skills/git-clean-worktrees inside it instead.

Cursor docs on skills

Run in a terminal (add --scope workspace to install it for the current workspace only):

gemini skills install https://github.com/ivy00johns/Skill-Madness.git --path skills/archive/git-clean-worktrees

Or copy the folder to ~/.gemini/skills/git-clean-worktrees yourself.

Gemini CLI docs on skills

For GitHub Copilot in VS Code, copy the skill folder to ~/.copilot/skills/git-clean-worktrees:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/ivy00johns/Skill-Madness.git "$tmp" &&
mkdir -p ~/.copilot/skills &&
rm -rf ~/.copilot/skills/git-clean-worktrees &&
cp -R "$tmp/skills/archive/git-clean-worktrees" ~/.copilot/skills/git-clean-worktrees &&
rm -rf "$tmp"

For one project only, put the folder in .github/skills/git-clean-worktrees inside it instead.

VS Code docs on skills

Get the folder:

git clone --depth 1 https://github.com/ivy00johns/Skill-Madness.git

Then copy Skill-Madness/skills/archive/git-clean-worktrees into your agent's skills folder as git-clean-worktrees. Agents that support SKILL.md read one folder per skill.

The SKILL.md format

What it does

Stale worktree detection
Identifies secondary worktrees whose branches or contents already match main.
Squash-merge awareness
Catches branches that were squash-merged, where ordinary merge checks miss them.
Uncommitted change comparison
Compares modified files in a worktree against main to spot phantom edits.
Pre-removal summary
Lists safe and unsafe worktrees with reasons before any deletion happens.
Guarded removal
Forces removal only after verification and never touches the main worktree.
Branch and metadata cleanup
Deletes merged local branches and prunes leftover worktree records.

Good for

  • Clean up leftover worktrees after several feature branches were merged
  • Check whether a worktree still holds unmerged work before deleting it
  • Remove phantom modified files that linger in old worktree directories
  • Tidy up branches and worktree metadata after a release

Git clean worktrees FAQ

When does Git clean worktrees trigger?

It triggers when the user mentions stale or leftover worktrees, worktree cleanup, or phantom modified files, and proactively when git worktree list shows entries beyond the main worktree.

What does Git clean worktrees need to run?

It uses Read and Bash tools and standard git commands, and expects the repository to have a main branch.

Will Git clean worktrees delete worktrees with unmerged work?

No. Worktrees that fail every check are flagged for the user, and removing unmerged work requires explicit confirmation.

Does Git clean worktrees handle squash-merged branches?

Yes. It compares the branch diff against main and treats an empty diff as safe, then may force-delete the branch.

How do I install Git clean worktrees in Claude Code?

Copy the skill folder from GitHub to ~/.claude/skills/git-clean-worktrees to use it in all your projects, or to .claude/skills/git-clean-worktrees in one project. Or install the skill-madness plugin, which includes it: /plugin marketplace add ivy00johns/Skill-Madness, then /plugin install skill-madness@skill-madness.

Does Git clean worktrees run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

Which AI agents can use Git clean worktrees?

Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose. Each reads skills in the SKILL.md format from its own folder; the install steps on this page list them.

What are the best alternatives to Git clean worktrees?

The closest alternatives to Git clean worktrees are Git post merge cleanup, Git branch cleanup, Git worktree, Gh PR merge worktree checkout trap and Check a stale branch is not undoing newer main work.

Where this comes from

  • Description: from the skill's own SKILL.md.

More from ivy00johns/Skill-Madness

  1. Finds merged and stale git branches, then deletes them only after your confirmation

    • Open source
    • 12 GitHub stars
    • Developer tools
  2. Bulk-scans a skill ecosystem for consistency, quality gaps and ownership conflicts

    • Open source
    • 12 GitHub stars
    • Developer tools
  3. Runs a single-skill quality review covering structure, triggering and output checks

    • Open source
    • 12 GitHub stars
    • Developer tools
  4. Turns skill review or audit reports into a prioritized list of concrete SKILL.md fixes

    • Open source
    • 12 GitHub stars
    • Developer tools
  5. Applies planned edits to SKILL.md files, validates them, and offers to sync

    • Open source
    • 12 GitHub stars
    • Developer tools
  6. Audits code against integration contracts to find mismatches before runtime tests

    • Open source
    • 12 GitHub stars
    • Developer tools