This repository has accumulated 2,878 remote branches, many of which are likely stale, merged, or abandoned. This guide explains how to safely clean up these branches.
A GitHub Actions workflow has been created to automate branch cleanup: .github/workflows/cleanup-stale-branches.yml
- Navigate to Actions tab in GitHub repository
- Select "Clean Up Stale Branches" workflow
- Click "Run workflow"
- Configure options:
- Dry run:
true(recommended first) - just lists branches without deleting - Batch size:
50(number of branches to process per run) - Branch filter: Choose from:
merged-prs- Branches from merged pull requests (safest)old-issue- Issue branches older than 6 monthsold-feature- Feature branches older than 6 monthsall- All branches older than 6 months
- Dry run:
-
First Run (Dry Run):
Dry run: true Filter: merged-prs Batch size: 50Review the artifact to see which branches would be deleted.
-
Second Run (Live):
Dry run: false Filter: merged-prs Batch size: 50This will delete the first 50 merged PR branches.
-
Repeat until all merged PR branches are cleaned up.
-
Review Other Categories: Then move on to
old-issueandold-featurefilters.
If you prefer manual cleanup, you can use these commands:
git fetch --all --prune
git branch -r | grep -v '\->' | wc -lgit push origin --delete <branch-name>Create a file delete_branches.sh:
#!/bin/bash
# List of branches to delete
BRANCHES=(
"664-potential-for-adding-a-system-role-to-usepollinationstext"
"668-make-terms-and-conditions-psychedelic"
# Add more branches here
)
for branch in "${BRANCHES[@]}"; do
echo "Deleting: $branch"
git push origin --delete "$branch" || echo "Failed to delete $branch"
sleep 0.5 # Rate limiting
doneMake it executable and run:
chmod +x delete_branches.sh
./delete_branches.shBased on analysis of branch naming patterns:
mainmasterdevelop/developmentstagingproduction
These are typically from pull requests. Safe to delete if:
- PR is merged
- PR is closed and abandoned
- Branch is older than 6 months
Safe to delete if:
- No open PR
- Older than 6 months
- Work is abandoned
Keep these:
- Branches with recent commits (< 1 month)
- Branches with open PRs
- Branches actively being worked on
The workflow includes several safety measures:
- Protected branch list - Hard-coded list of branches that can never be deleted
- Dry run mode - Test before actually deleting
- Batch processing - Limit number of branches deleted per run
- Artifacts - Saves list of deleted branches for review
- Rate limiting - Delays between deletions to avoid API limits
To check if a branch's PR was merged:
# For issue-numbered branch (e.g., 664-feature)
gh pr view 664 --json state,mergedAt
# Get all merged PRs
gh pr list --state merged --limit 1000 --json number,headRefNameFor a comprehensive analysis before cleanup:
#!/bin/bash
# Count branches by type
echo "=== Branch Analysis ==="
echo ""
# Total branches
total=$(git branch -r | grep -v '\->' | wc -l)
echo "Total branches: $total"
# Issue-numbered branches
issue_branches=$(git branch -r | grep -v '\->' | grep -E 'origin/[0-9]+-' | wc -l)
echo "Issue-numbered branches: $issue_branches"
# Feature branches
feature_branches=$(git branch -r | grep -v '\->' | grep -E 'origin/(feature|fix|hotfix|bugfix)/' | wc -l)
echo "Feature branches: $feature_branches"
# Copilot branches
copilot_branches=$(git branch -r | grep -v '\->' | grep -E 'origin/copilot/' | wc -l)
echo "Copilot branches: $copilot_branches"
# Add-* branches (project additions)
add_branches=$(git branch -r | grep -v '\->' | grep -E 'origin/add-' | wc -l)
echo "Add-* branches: $add_branches"
echo ""
echo "=== Recommendations ==="
echo "1. Start with merged PR branches (issue-numbered)"
echo "2. Then clean up old feature branches"
echo "3. Finally review other categories"Track cleanup progress:
# Before cleanup
git fetch --all --prune
git branch -r | grep -v '\->' | wc -l
# After each cleanup run
git fetch --all --prune
git branch -r | grep -v '\->' | wc -l- The branch has branch protection rules enabled
- Disable protection rules in Settings > Branches first
- Branch was already deleted
- Run
git fetch --all --pruneto sync
- Use smaller batch sizes
- Run multiple times
- Use different filters for different categories
- Always start with dry run mode
- Review the artifact from dry run before live deletion
- Start with merged PR branches (safest category)
- Process in batches to avoid overwhelming the system
- Keep audit trail - Download artifacts from each run
- Coordinate with team before major cleanups
- Run during low-activity periods to minimize disruption
After cleanup, you should have:
- Active branches only: Recent work and open PRs
- Protected branches: Main, develop, etc.
- Improved performance: Faster git operations
- Better hygiene: Easier to find relevant branches
Target: Reduce from 2,878 branches to approximately 50-100 active branches.