Skip to main content
ClaudeWave
Skill82.2k repo starsupdated 3d ago

cleanup-git-worktrees

Audit and safely clean Git worktrees and stale local branches. Use when asked to list worktrees, identify completed or merged branches, remove worktrees whose remote branches are gone, prune stale worktree registrations, or clean local branches without losing uncommitted work.

Install in Claude Code
Copy
git clone --depth 1 https://github.com/lobehub/lobehub /tmp/cleanup-git-worktrees && cp -r /tmp/cleanup-git-worktrees/.agents/skills/cleanup-git-worktrees ~/.claude/skills/cleanup-git-worktrees
Then start a new Claude Code session; the skill loads automatically.

SKILL.md

# Cleanup Git Worktrees

Use the bundled script to make classification deterministic. Treat cleanup as a destructive action: audit first, show the exact candidates, and obtain explicit user approval before applying deletion unless the user's current request already names the exact targets.

## Workflow

1. Run the audit from any worktree in the repository:

   ```bash
   bash .agents/skills/cleanup-git-worktrees/scripts/cleanup.sh audit --fetch --base origin/canary
   ```

   Omit `--fetch` only when network access is unavailable; disclose that remote state may be stale.

2. Interpret classifications:

   - `protect-dirty`: preserve; it has modified or untracked files.
   - `protect-current`: preserve; never remove the worktree running the command.
   - `candidate-merged`: clean and fully contained in the base branch.
   - `candidate-gone`: clean and its configured upstream was pruned. This is stale, but not proof of merge after squash/rebase.
   - `review-no-upstream`: clean but lacks an upstream and is not contained in the base.
   - `active`: retain unless the user explicitly identifies it as finished.
   - `protected-branch`: never delete `main`, `canary`, or the base branch.

3. Present a compact table containing path, branch, dirty count, upstream state, base containment, and classification. Keep `candidate-merged` separate from `candidate-gone`; do not describe `[gone]` as proof of merge.

4. After approval, pass exact branch names to cleanup:

   ```bash
   bash .agents/skills/cleanup-git-worktrees/scripts/cleanup.sh clean \
     --base origin/canary \
     --branch feat/example \
     --branch fix/example \
     --apply
   ```

   Without `--apply`, cleanup is a dry run. The script re-audits every target immediately before deletion.

5. Run the audit again and report:

   - removed worktrees and branches;
   - retained dirty/current targets;
   - remaining worktree and local-branch counts;
   - any partial deletion or Git error.

## Safety rules

- Never discard dirty worktrees merely because their branch is merged or `[gone]`.
- Never use recursive deletion to bypass `git worktree remove`. If Git partially removes a directory, stop and inspect the exact path before deciding how to recover.
- Never force-delete a branch that is neither contained in the base nor `[gone]` through this script.
- A deleted upstream indicates staleness, not necessarily merge. Use GitHub PR state when the distinction matters.
- Preserve unrelated user changes and concurrent worktrees.
- Prefer `git fetch --prune origin` before classification and `git worktree prune` only for registrations whose directories are already missing.

## Script

Use `scripts/cleanup.sh`; do not recreate its parsing and guard logic ad hoc unless the repository layout makes it unusable.
add-provider-docSkill

Add documentation for a new AI provider — usage docs, env vars, Docker config, image resources.

add-setting-envSkill

Add server-side environment variables that control default values for user settings.

agent-runtime-hooksSkill

Agent runtime lifecycle hooks. Use for before/after tool or step hooks, tool mocks, human intervention, sub-agent calls, context compression, evals, callAgent, or lifecycle events.

agent-signalSkill

Build or extend LobeHub Agent Signal pipelines. Use for signal sources, signal/action types, policies, middleware, workflow handoff, dedupe, scope behavior, or observability.

agent-tracingSkill

Agent tracing CLI for execution snapshots. Use for agent-tracing, traces, snapshots, LLM call inspection, context engine data, agent step analysis, execution debugging, or pulling remote/production traces ("拉线上 tracing") by operation id. Also the first stop for debugging agent tool calls — wrong or missing tool_calls, unexpected tool arguments or results, which tools were available at a step, or why a tool ran where it did.

builtin-toolSkill

Build LobeHub builtin tool packages. Use when adding agent-callable tools, manifests, executors, runtimes, inspectors, renders, placeholders, streaming, interventions, portals, or tool registries.

chat-sdkSkill

Build multi-platform chat bots with the chat SDK. Use for Slack, Teams, Google Chat, Discord, GitHub, Linear bots, webhooks, mentions, slash commands, cards, modals, or streaming responses.

cli-backend-testingSkill

>