Skip to content
Back to skills

Clean Branches

ASecurity

Delete local branches whose remote has been deleted ([gone]), including their worktrees. Use for branch cleanup after merging PRs.

  • 901 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added May 27, 2026
developmentgobashgit

Security analysis

A100/100

Scanned May 27, 2026

npx -y skills add kdlbs/kandev --skill clean-branches --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Clean Branches?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Clean Branches
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kdlbs-clean-branches/badge)](https://www.skillsdirectory.com/skills/kdlbs-clean-branches)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: clean-branches
description: Delete local branches whose remote has been deleted ([gone]), including their worktrees. Use for branch cleanup after merging PRs.
---

# Clean Branches

Remove local branches that have been deleted on the remote (marked `[gone]`), including any associated git worktrees.

## Steps

1. **Fetch and prune remote refs:**
   ```bash
   git fetch --prune
   ```

2. **List branches to identify [gone] status:**
   ```bash
   git branch -v
   ```
   Branches with a `+` prefix have associated worktrees that must be removed before deletion.

3. **List worktrees for reference:**
   ```bash
   git worktree list
   ```

4. **Remove worktrees and delete [gone] branches:**
   ```bash
   git branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do
     echo "Processing branch: $branch"
     worktree=$(git worktree list | grep "\\[$branch\\]" | awk '{print $1}')
     if [ ! -z "$worktree" ] && [ "$worktree" != "$(git rev-parse --show-toplevel)" ]; then
       echo "  Removing worktree: $worktree"
       git worktree remove --force "$worktree"
     fi
     echo "  Deleting branch: $branch"
     git branch -D "$branch"
   done
   ```

5. **Report** which branches and worktrees were removed. If none were `[gone]`, say no cleanup was needed.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.