---
name: Clean Branches
slug: clean-branches
category: DevOps
description: Clean Branches removes local Git branches whose remote tracking refs are gone, including any associated worktrees. Use it after merges or PR cleanup to prune stale branches safely.
github: "https://github.com/kdlbs/kandev/tree/main/.agents/skills/clean-branches"
language: Go
stars: 679
forks: 95
install: "npx degit https://github.com/kdlbs/kandev/tree/main/.agents/skills/clean-branches ~/.claude/skills/clean-branches"
installs_to: ~/.claude/skills/clean-branches
source_path: .agents/skills/clean-branches/SKILL.md
collection_size: 25
category_size: 798
collection_url: "https://dirskills.com/collections/kdlbs/kandev"
added: 2026-08-23T05:21:06.167Z
last_synced: 2026-08-23T05:21:06.167Z
canonical_url: "https://dirskills.com/skills/clean-branches"
---

# Clean Branches

Clean Branches removes local Git branches whose remote tracking refs are gone, including any associated worktrees. Use it after merges or PR cleanup to prune stale branches safely.

**Install:**

```bash
npx degit https://github.com/kdlbs/kandev/tree/main/.agents/skills/clean-branches ~/.claude/skills/clean-branches
```

## README

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