CLSkills
Git & Version Controlbeginner

Branch Cleanup

Share

Find and delete merged/stale local and remote branches

Branch Cleanup

Find and delete merged/stale local and remote branches

Find and safely delete merged and stale git branches.

Step-by-Step

  1. List all local branches with tracking info:
git branch -vv
  1. Find local branches already merged into main:
git branch --merged main | grep -v "\* \|main\|master\|develop"
  1. Find stale remote branches (no commits in 30+ days):
git for-each-ref --sort=committerdate refs/remotes/ \
  --format='%(committerdate:short) %(refname:short)' | head -30
  1. Show the list to the user and ask for confirmation before deleting.

  2. Delete confirmed branches:

git branch -d branch-name                 # local (safe — refuses unmerged)
git push origin --delete branch-name      # remote
git remote prune origin                   # clean stale tracking refs

Protected Branches (never delete)

main, master, develop, staging, production, release/*

Safety

  • Always use -d (not -D) — it refuses to delete unmerged branches
  • Show git log main..branch-name --oneline for any branch the user is unsure about

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
gitbranchescleanup

Install command:

curl -o ~/.claude/skills/branch-cleanup.md https://claude-skills-hub.vercel.app/skills/git/branch-cleanup.md