Skip to content

commandGit and releases

Branch Cleanup

Lists merged and stale local branches, shows exactly what would be removed and deletes only the ones you approve.

npx workspacetemplates add branch-cleanup --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/branch-cleanup.md14 lines · 1.2 KBFile
---
description: Lists merged and stale local branches, shows exactly what would be removed and deletes only the ones you approve.
argument-hint: "[staleness window, default 90d]"
---

Clean up local branches. Options from the user: $ARGUMENTS (for example a staleness window like 90d).

1. Run `git fetch --prune` and `git branch -vv`. Identify the default branch and the current branch; both are protected, with `main`, `master`, `develop` and `release*`.
2. Group the rest: **merged** (`git branch --merged <default>`), **gone** (upstream deleted), **stale** (no commit in 90 days or the given window) and **active**.
3. For each non-merged candidate show last commit date, author, and the count of commits not in the default branch (`git rev-list --count <default>..<branch>`). Mark any with unpushed commits as at risk.
4. Print the plan as a table of branch, group, last commit and risk, with the exact command for each deletion.
5. Delete nothing yet. Ask which groups to remove. Use `git branch -d` for merged ones; use `-D` only for branches the user names one by one after seeing their unmerged commits. Never touch remote branches.

After deleting, print the commit hash of each removed branch so it can be restored with `git branch <name> <hash>`.

What it does

Old branches pile up until nobody knows which ones still matter. This command separates branches already merged from those that are simply old, and shows unpushed work that would be lost.

Nothing is deleted before you confirm the list. Protected branches such as main, develop and the current branch are never touched.

Example

you › /branch-cleanup 60d

agent › Merged (safe): feat/login-form, fix/typo-footer, chore/deps-march.
Gone upstream: feat/export-csv (2 unmerged commits, at risk).
Stale: spike/graphql, 140 days, 9 commits not in main.
Delete the merged group? Restore tip: git branch feat/login-form 3fa91c2.

Questions

Can I recover a deleted branch?
Yes. It prints each removed branch tip hash, and git reflog also keeps it for a while.
Does it delete remote branches?
No, only local ones. Remote cleanup is left to you or your hosting platform.
  • Changelog Entry

    CommandGit and releases

    Free
  • Cherry Pick Fix

    CommandGit and releases

    Free
  • Commit Message

    CommandGit and releases

    Free
  • Fixup Commits

    CommandGit and releases

    Free