# Git clean worktrees

> Git clean worktrees is an open-source agent skill for developer tools. Finds and removes git worktrees whose changes are already merged into main. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Goose
- **Website:** https://github.com/ivy00johns/Skill-Madness/tree/main/skills/archive/git-clean-worktrees
- **Repository:** https://github.com/ivy00johns/skill-madness
- **GitHub stars:** 12
- **License:** MIT
- **Skill name:** git-clean-worktrees
- **Collection:** https://github.com/ivy00johns/Skill-Madness
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-12-03

## About

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.

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/ivy00johns/Skill-Madness/tree/HEAD/skills/archive/git-clean-worktrees)
- [License: MIT](https://github.com/ivy00johns/Skill-Madness/tree/HEAD/skills/archive/git-clean-worktrees)

## Features

- **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

## Install

### Claude Code

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

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

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

```sh
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.

### Codex

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

```sh
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.

### Cursor

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

```sh
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.

### Gemini CLI

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

```sh
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.

### VS Code

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

```sh
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.

### Other agents

Get the folder:

```sh
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.

## 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.

## Alternatives

- [Git post merge cleanup](https://ai-powered.com/skills/git-post-merge-cleanup): Git post merge cleanup is an open-source agent skill for developer tools.
- [Git branch cleanup](https://ai-powered.com/skills/git-branch-cleanup): Git branch cleanup is an open-source agent skill for developer tools.
- [Git worktree](https://ai-powered.com/skills/git-worktree): Git worktree is an open-source agent skill for developer tools.
- [Gh PR merge worktree checkout trap](https://ai-powered.com/skills/gh-pr-merge-worktree-checkout-trap): Gh PR merge worktree checkout trap is an open-source agent skill for developer tools.
- [Check a stale branch is not undoing newer main work](https://ai-powered.com/skills/check-a-stale-branch-is-not-undoing-newer-main-work): Check a stale branch is not undoing newer main work is an open-source agent skill for developer tools.
- [Worktrees](https://ai-powered.com/skills/worktrees): Worktrees is an open-source agent skill for developer tools.
- [Git auto maintenance recurring worktree index lock](https://ai-powered.com/skills/git-auto-maintenance-recurring-worktree-index-lock): Git auto maintenance recurring worktree index lock is an open-source agent skill for developer tools.
- [Harness read write base repo path in worktree stale tree](https://ai-powered.com/skills/harness-read-write-base-repo-path-in-worktree-stale-tree): Harness read write base repo path in worktree stale tree is an open-source agent skill for developer tools.

---
Source: https://ai-powered.com/skills/git-clean-worktrees
