Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Git & Version Controlbeginner

Branch Cleanup

Share

Find and delete merged/stale local and remote branches

Works with OpenClaude

You are a Git branch management expert. The user wants to safely identify and delete merged or stale local and remote branches.

What to check first

  • Run git branch -vv to see local branches and their tracking status
  • Run git branch -r to list all remote-tracking branches
  • Run git remote prune origin --dry-run to see which remote branches are gone
  • Check git log --oneline -1 on each branch to see age and last commit

Steps

  1. Fetch latest remote changes with git fetch --all --prune to sync remote-tracking branches and remove deleted remote refs
  2. List all local branches merged into current branch with git branch --merged — these are safe to delete
  3. List all local branches merged into main/master with git branch --merged main (or your default branch name)
  4. Delete merged local branches one by one using git branch -d branch-name or batch delete with git branch -d $(git branch --merged | grep -v "\*")
  5. Find stale remote-tracking branches (typically >30 days old) by checking commit dates with git for-each-ref --sort=-committerdate refs/remotes/ --format='%(refname:short) %(committerdate:short)'
  6. Delete remote-tracking branches that no longer exist upstream with git branch -r --list and identify dead refs
  7. Delete specific remote branches with git push origin --delete branch-name (requires permission)
  8. Verify cleanup with git branch -a to confirm branches are gone

Code

#!/bin/bash
# Branch cleanup script — finds and removes merged/stale local and remote branches

set -e

MAIN_BRANCH="${1:-main}"
STALE_DAYS="${2:-60}"

echo "=== Fetching latest from all remotes ==="
git fetch --all --prune

echo ""
echo "=== Local branches merged into $MAIN_BRANCH ==="
MERGED_BRANCHES=$(git branch --merged "$MAIN_BRANCH" | grep -v "\*" | grep -v "^ *$MAIN_BRANCH$")

if [ -z "$MERGED_BRANCHES" ]; then
    echo "No merged branches found."
else
    echo "Found merged branches:"
    echo "$MERGED_BRANCHES"
    echo ""
    read -p "Delete these merged local branches? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "$MERGED_BRANCHES" | xargs -r git branch -d
        echo "Deleted merged branches."
    fi
fi

echo ""
echo "=== Remote-tracking branches (deleted upstream) ==="
DELETED_REMOTE=$(git branch -r | grep -v HEAD)
echo "Checking for dead remote-tracking refs..."
git remote prune origin --dry-run

echo ""
echo "=== Stale branches (older than $ST

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

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

Related Git & Version Control Skills

Other Claude Code skills in the same category — free to download.

Want a Git & Version Control skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.