Find and delete merged/stale local and remote branches
✓Works with OpenClaudeYou 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 -vvto see local branches and their tracking status - Run
git branch -rto list all remote-tracking branches - Run
git remote prune origin --dry-runto see which remote branches are gone - Check
git log --oneline -1on each branch to see age and last commit
Steps
- Fetch latest remote changes with
git fetch --all --pruneto sync remote-tracking branches and remove deleted remote refs - List all local branches merged into current branch with
git branch --merged— these are safe to delete - List all local branches merged into main/master with
git branch --merged main(or your default branch name) - Delete merged local branches one by one using
git branch -d branch-nameor batch delete withgit branch -d $(git branch --merged | grep -v "\*") - 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)' - Delete remote-tracking branches that no longer exist upstream with
git branch -r --listand identify dead refs - Delete specific remote branches with
git push origin --delete branch-name(requires permission) - Verify cleanup with
git branch -ato 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
Related Git & Version Control Skills
Other Claude Code skills in the same category — free to download.
Smart Commit
Generate conventional commit messages by analyzing staged changes
Git Undo
Safely undo the last git operation (commit, merge, rebase, etc.)
Changelog Generator
Generate CHANGELOG.md from git history using conventional commits
Conflict Resolver
Analyze and suggest resolutions for merge conflicts
Git Bisect Helper
Automate git bisect to find the commit that introduced a bug
PR Description
Generate detailed PR descriptions from branch diff
Commit Splitter
Split a large commit into smaller, logical commits
Git Hooks Setup
Set up pre-commit, pre-push, and commit-msg hooks
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.