agentleFS
DScardini91DScardini91/agentic-os/.claude/skills/branch-cleanup/SKILL.md

branch-cleanup

Skill2 starsChanged 5 months agoMITView on GitHub

Sweep orphan remote branches (no open PR), group by age and author, identify safe deletes, and propose a batch delete via gh CLI. Reduces repo noise without losing history — commits stay in reflog and main.

  • Commits and pushes

6 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 6 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If a number is returned → has an open PR → do not include in the cleanup list.

    line 46
  2. Very specific:

    If dry_run=true, stop after Step 5.

    line 81
  3. Very specific:

    If dry_run=false, ask: *"Delete N safe branches?

    line 82
  4. Distinctive:

    Never auto-delete 🟡 (unique commits) — those require explicit principal approval per branch.

    line 89

“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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Skill: branch-cleanup

When to use

The repo has accumulated remote branches without open PRs — leftovers from squash-merged PRs, abandoned work, or old sessions.

NOT for:

  • Deleting a local branch — use git branch -D directly.
  • Deleting a branch with an open PR — close the PR first.
  • Force-deleting a branch with unique unmerged commits — escalate to the principal (risk of loss).

Inputs

  • repo_path (optional) — defaults to cwd.
  • age_days (optional) — last-commit age threshold, default 14.
  • dry_run (optional) — defaults to true. Lists without deleting. false deletes after confirmation.

Sequence

Step 1 — Collect remote branches

cd <repo_path>
git fetch --prune origin
git branch -r | grep -v HEAD | grep -vE 'origin/(main|master)$'

Step 2 — Filter out branches with open PRs

For each remote branch, check open PR status:

gh pr list --head <branch_name> --state open --json number --jq '.[].number'

If a number is returned → has an open PR → do not include in the cleanup list.

Step 3 — Collect metadata per branch

git log <branch> -1 --pretty=format:'%cs|%an|%H'
git merge-base --is-ancestor <branch_sha> origin/main
# exit 0 = merged ancestor, exit 1 = unique commits

Step 4 — Categorize

CategoryCriterionDefault action
🟢 Safe deleteMerged into main + age ≥ age_daysPropose delete
🟡 Stale but uniqueNot merged + age ≥ 30 daysList for principal to decide
🔴 RecentAge < age_daysSkip (likely active work)

Step 5 — Structured output

## Branch cleanup — repo <name>

🟢 Safe to delete (merged + stale ≥ {age_days}d): N
  - <branch>  · {age}d  · {author}  · merged

🟡 Unique commits, stale ≥ 30d: M
  - <branch>  · {age}d  · {author}  · {n} unique commits

🔴 Recent (skip): K

Total candidates: N+M

Step 6 — Confirm + execute

If dry_run=true, stop after Step 5. Hand off the table to the principal. If dry_run=false, ask: "Delete N safe branches? (yes/no)". On yes:

for branch in <safe_list>; do
  git push origin --delete "$branch"
done

Never auto-delete 🟡 (unique commits) — those require explicit principal approval per branch.


Anti-patterns

  • Deleting a branch with unique commits because "it's old" — those commits exist only on that branch; deletion loses them. Always require explicit per-branch approval.
  • Bulk-delete without showing the table first — the principal needs to scan the list, not trust a count.
  • Including main/master in the candidate list — defensive grep filter required.

Why it matters

git branch -r output bloats over time. A repo with 50+ stale branches makes gh pr list and IDE branch pickers noisy. Periodic cleanup keeps the working surface small without losing history (everything stays in the reflog and on main if merged).

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.