branch-cleanup
Skill16 starsChanged 10 months agoApache-2.0View on GitHub
Clean up local branches after PR merges. Syncs main with origin, identifies branches with merged PRs, and proposes safe deletion. Use when the user asks to 'clean up branches', 'delete merged branches', 'sync branches', or mentions branch cleanup.
- Commits and pushes
6 other projects use a version of this.
Adopted by
Most recently taken up by
- jaskirat1616/grok-skillstheir own version, first seen Nov 1, 2026
- gspencergoog/skillstheir own version, first seen Oct 16, 2026
- stealth-factory/skillstheir own version, first seen Oct 13, 2026
- lwhsu/freebsd-claude-skillstheir own version, first seen Aug 5, 2026
- Jinbae10/skillstheir own version, first seen Jul 7, 2026
- DScardini91/agentic-ostheir own version, first seen Jun 26, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- jaskirat1616/grok-skills★ 22same name
- stealth-factory/skills★ 5same name
- DScardini91/agentic-os★ 2same name
- gspencergoog/skills★ 2same name
- lwhsu/freebsd-claude-skills★ 2same name
- Jinbae10/skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository7 other files it gives its agents.
- .cursor/rules/commit-standards.mdcCursor rule
- .cursor/rules/cursor-rules-guidelines.mdcCursor rule
- .cursor/rules/development-workflow.mdcCursor rule
- .cursor/rules/linear-ai-todo-integration.mdcCursor rule
- .cursor/rules/linear-document-workflow.mdcCursor rule
- .cursor/rules/linear-release-workflow.mdcCursor rule
- release-workflowSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
17 rules. 0 are common, 16 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 6 edited versions we read keep these.
16
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- could be automatic: a setting could enforce it
- Distinctive: line 12
When user asks to "clean up branches" or "delete merged branches":
- Distinctive: line 17
If not on main: Ask user: "Should I switch to main?
- Distinctive: line 31
If merged PR found: Add to cleanup list
- Distinctive: line 37
If user declines: STOP.
- Distinctive: line 60
Wait for user confirmation
- Distinctive: line 63
Never switch branches with uncommitted changes
- Distinctive: line 64
Check: git status --porcelain (must be empty)
- Distinctive: line 78
Do NOT attempt merge or rebase
- Distinctive: line 139
If no branches qualify for cleanup:
- Distinctive: line 147
If branch has commits not in main but PR is merged:
- Distinctive: line 160
Only delete local branches, never remote refs:
- Very specific: line 164
Use git push origin --delete {branch} only if user explicitly requests remote deletion.
- Distinctive: line 172
WAIT for user input
- Distinctive: line 227
If user asks for "preview" or "dry run":
- Distinctive: line 234
If user explicitly asks to "delete remote branches too":
- Distinctive: line 235
After local deletion succeeds
- Distinctive: line 246
Repository: Must be a git repository with GitHub remote
“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Branch Cleanup
Automated workflow to sync main with origin and clean up local branches that have merged PRs.
Quick Start
When user asks to "clean up branches" or "delete merged branches":
- Pre-flight checks:
git fetch origin(update remote refs)git branch --show-current(verify current branch)- If not on main: Ask user: "Should I switch to main? (Current: {branch})"
- Sync main:
git checkout maingit pull origin main --ff-only- If conflicts: ⚠️ Stop and report "Main has conflicts. Resolve manually."
- List local branches:
git branch --format='%(refname:short)'- Filter out:
main,master,develop,staging,production,archive* - If no branches: Report "No local branches to clean up" and STOP.
- Check PR status for each branch:
- Get PR for branch:
gh pr list --head {branch} --state merged --json number,title,url - If merged PR found: Add to cleanup list
- If no PR or not merged: Skip
- Get PR for branch:
- Present cleanup list:
- Show table with: branch name, PR number, PR title, PR URL
- Ask user: "Delete these {count} branches? (y/n)"
- If user declines: STOP.
- Delete branches:
- For each approved branch:
git branch -d {branch}(safe delete)- If fails with unmerged warning: Show warning and skip
- If succeeds: Report "✅ Deleted {branch}"
- Final summary: "Deleted {count} branches, skipped {count} unmerged branches"
- For each approved branch:
Workflow Details
Step 1: Pre-flight Checks
# Fetch latest remote refs
git fetch origin
# Check current branch
CURRENT_BRANCH=$(git branch --show-current)If not on main:
- Show: "Currently on
{CURRENT_BRANCH}. Switch to main?" - Wait for user confirmation
Safety:
- Never switch branches with uncommitted changes
- Check:
git status --porcelain(must be empty)
Step 2: Sync Main
# Switch to main
git checkout main
# Fast-forward merge only (no conflicts)
git pull origin main --ff-onlyIf fast-forward fails:
- ⚠️ Stop with error: "Main branch has diverged from origin. Manual resolution required."
- Do NOT attempt merge or rebase
Step 3: Identify Merged Branches
For each local branch:
# Get merged PR for branch
gh pr list --head {branch} --state merged --json number,title,url --limit 1Branch categories:
| Status | Action |
| Merged PR found | Add to cleanup list |
| Open PR found | Skip (not merged) |
| No PR found | Skip (might be local work) |
| Protected branch | Skip (main, master, etc.) |
Protected branches:
main,master,develop,staging,production- Any branch starting with
archive(e.g.,archive-2024,archive/old) - Any branch matching
origin/*
Step 4: Present Cleanup List
Format as markdown table:
## Branches with merged PRs
| Branch | PR | Title |
|--------|-----|-------|
| fxstein/AIT-12-feature | [#45](url) | Feature implementation |
| oliver/ait-8-bugfix | [#38](url) | Fix login bug |
Total: 2 branchesUser prompt: "Delete these branches? They have merged PRs and are safe to remove. (y/n)"
Step 5: Safe Deletion
# Safe delete (only if fully merged)
git branch -d {branch}If git branch -d fails:
- Shows: "The branch '{branch}' is not fully merged."
- Action: Skip and report to user
- Reason: Prevents data loss
Force delete (-D) is NEVER used automatically.
Edge Cases
No Merged Branches
If no branches qualify for cleanup:
✅ All local branches are up to date.
No merged branches found for cleanup.Partially Merged Branches
If branch has commits not in main but PR is merged:
git branch -dwill fail (expected)- Report: "⚠️ Skipped {branch}: has unmerged commits (possible rebase)"
- User can manually verify with:
git log main..{branch}
Archive Branches
Branches starting with archive are preserved:
- ❌ Skip:
archive-2024,archive/backup,archive-old-features - Reason: Archive branches contain historical work that should be manually managed
Remote Tracking Branches
Only delete local branches, never remote refs:
- ✅ Delete:
fxstein/AIT-12-feature - ❌ Skip:
origin/fxstein/AIT-12-feature
Use git push origin --delete {branch} only if user explicitly requests remote deletion.
Error Handling
If ANY error occurs:
- STOP IMMEDIATELY
- Report the specific error
- Show current state: branch name, operation attempted
- WAIT for user input
Common errors:
| Error | Cause | Action |
| "Not on main" | Current branch not main | Ask to switch |
| "Fast-forward failed" | Main diverged from origin | Stop, manual resolution |
| "gh command not found" | GitHub CLI not installed | Stop, ask user to install |
| "Branch not fully merged" | Branch has unmerged commits | Skip branch |
Safety Guarantees
- ✅ Never force-delete (
-D) branches - ✅ Never delete without user confirmation
- ✅ Never modify remote branches
- ✅ Never switch branches with uncommitted changes
- ✅ Always use
--ff-onlyfor main sync
Output Format
Success
🔄 Syncing main with origin...
✅ Main is up to date
📋 Found 3 branches with merged PRs:
- fxstein/AIT-12-feature (PR #45)
- oliver/ait-8-bugfix (PR #38)
- fxstein/AIT-10-docs (PR #42)
🗑️ Deleting branches...
✅ Deleted fxstein/AIT-12-feature
✅ Deleted oliver/ait-8-bugfix
✅ Deleted fxstein/AIT-10-docs
✨ Cleanup complete: 3 branches deletedWith Skipped Branches
🗑️ Deleting branches...
✅ Deleted fxstein/AIT-12-feature
⚠️ Skipped fxstein/AIT-15-wip: not fully merged
✅ Deleted oliver/ait-8-bugfix
✨ Cleanup complete: 2 deleted, 1 skippedAdditional Features
Dry Run Mode
If user asks for "preview" or "dry run":
- Execute steps 1-4 only
- Show cleanup list
- STOP before deletion
Delete Remote Branches
If user explicitly asks to "delete remote branches too":
- After local deletion succeeds
- For each deleted branch:
git push origin --delete {branch} - Report: "✅ Deleted {branch} (local + remote)"
Requirements
- Git: Version 2.0+
- GitHub CLI (gh): Required for PR status checks
- Repository: Must be a git repository with GitHub remote
Forbidden Actions
- ❌ Never use
git branch -D(force delete) - ❌ Never delete branches without user confirmation
- ❌ Never modify
main,master, or protected branches - ❌ Never attempt merge/rebase to resolve conflicts
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

