Git Repository Cleanup
Repository hygiene: prune branches, detect stale work, and check repo health.
Parse the user's input to determine the action:
$git-cleanup or $git-cleanup branches — find and prune merged/stale branches
$git-cleanup stale [days] — branches with no commits in N days (default 30)
$git-cleanup health — large files, dangling refs, worktrees, repo size
$git-cleanup all — scan all git repos under ~/Projects/
Prerequisites
- Verify we're in a git repository (
git rev-parse --is-inside-work-tree) — except for all action which scans directories
- If not in a git repo (and not using
all), tell the user and stop
Protected Branches
The following branches are NEVER suggested for deletion: main, master, develop
Action: branches (default)
- Fetch latest remote state:
git fetch --prune
- Identify the default branch (
git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null or fall back to main/master)
- Find merged branches:
git branch --merged <default> — local branches already merged into default
- Exclude protected branches and current branch
- Find branches with deleted remotes:
git branch -vv — look for [origin/...: gone] entries
- Present findings as a dry-run preview:
- List branches that can be safely deleted (merged)
- List branches whose remote is gone
- Show last commit date and message for each
- Ask the user which branches to delete (or "all listed")
- On confirmation, delete with
git branch -d <branch> (safe delete)
- If a branch fails
-d (unmerged), warn the user and offer -D with explicit explanation that commits may be lost — only proceed with user confirmation
Action: stale [days]
- Default to 30 days if no argument provided
- For each local branch (excluding protected branches):
- Get last commit date:
git log -1 --format='%ci' <branch>
- Calculate age in days
- List branches older than the threshold, sorted by age (oldest first)
- Show for each: branch name, last commit date, last commit message, age in days
- Present as dry-run preview — ask before deleting
- On confirmation, use
git branch -d first; offer -D only with warning if unmerged
Action: health
Run a comprehensive repository health check:
Repository size:
.git directory size: du -sh .git
- Working tree size:
du -sh . --exclude=.git (or equivalent)
- Object count:
git count-objects -vH
Large files (top 10 by size in history):
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print $3, $4}' | sort -rn | head -10
- Flag files over 10MB
Dangling objects:
git fsck --no-reflogs 2>&1 — look for dangling commits, blobs, trees
- Report count and suggest
git gc if significant
Worktrees:
git worktree list — show active worktrees
Remote status:
git remote -v — list remotes
- Check if local is ahead/behind remote
Stale tracking branches:
git remote prune origin --dry-run — branches that would be pruned
Present findings with recommendations
If cleanup is recommended (gc, prune), show the commands but always ask before running them
Action: all
- Find all git repositories under
~/Projects/:
find ~/Projects -maxdepth 3 -name .git -type d
- For each repository found:
a. Show repo name and path
b. Run the branches check (merged + gone remotes)
c. Show summary: N merged branches, M gone-remote branches
- Per-repo confirmation — ask before deleting branches in each repo
- Show overall summary at the end
Safety Rules
- Never delete without confirmation — always preview first
- Never delete protected branches (main, master, develop)
- Never
git push origin --delete on protected branches
- Never run
git gc without asking
- Always preview first — every action starts as a dry run
- Per-repo confirmation for
all — don't batch-delete across repos
- Use
git branch -d (safe delete) first; only offer -D with explicit warning
1---2name: git-cleanup3description: Repository hygiene — prune merged/stale branches, detect orphans, repo health checks. Use when cleaning up branches, checking repo health, or scanning projects.4---56# Git Repository Cleanup78Repository hygiene: prune branches, detect stale work, and check repo health.910Parse the user's input to determine the action:11- `$git-cleanup` or `$git-cleanup branches` — find and prune merged/stale branches12- `$git-cleanup stale [days]` — branches with no commits in N days (default 30)13- `$git-cleanup health` — large files, dangling refs, worktrees, repo size14- `$git-cleanup all` — scan all git repos under ~/Projects/1516## Prerequisites17181. Verify we're in a git repository (`git rev-parse --is-inside-work-tree`) — except for `all` action which scans directories192. If not in a git repo (and not using `all`), tell the user and stop2021## Protected Branches2223The following branches are NEVER suggested for deletion: `main`, `master`, `develop`2425## Action: branches (default)26271. Fetch latest remote state: `git fetch --prune`282. Identify the default branch (`git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null` or fall back to `main`/`master`)293. Find merged branches:30 - `git branch --merged <default>` — local branches already merged into default31 - Exclude protected branches and current branch324. Find branches with deleted remotes:33 - `git branch -vv` — look for `[origin/...: gone]` entries345. Present findings as a **dry-run preview**:35 - List branches that can be safely deleted (merged)36 - List branches whose remote is gone37 - Show last commit date and message for each386. Ask the user which branches to delete (or "all listed")397. On confirmation, delete with `git branch -d <branch>` (safe delete)408. If a branch fails `-d` (unmerged), warn the user and offer `-D` with explicit explanation that commits may be lost — only proceed with user confirmation4142## Action: stale [days]43441. Default to 30 days if no argument provided452. For each local branch (excluding protected branches):46 - Get last commit date: `git log -1 --format='%ci' <branch>`47 - Calculate age in days483. List branches older than the threshold, sorted by age (oldest first)494. Show for each: branch name, last commit date, last commit message, age in days505. Present as dry-run preview — ask before deleting516. On confirmation, use `git branch -d` first; offer `-D` only with warning if unmerged5253## Action: health5455Run a comprehensive repository health check:56571. **Repository size**:58 - `.git` directory size: `du -sh .git`59 - Working tree size: `du -sh . --exclude=.git` (or equivalent)60 - Object count: `git count-objects -vH`61622. **Large files** (top 10 by size in history):63 - `git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print $3, $4}' | sort -rn | head -10`64 - Flag files over 10MB65663. **Dangling objects**:67 - `git fsck --no-reflogs 2>&1` — look for dangling commits, blobs, trees68 - Report count and suggest `git gc` if significant69704. **Worktrees**:71 - `git worktree list` — show active worktrees72735. **Remote status**:74 - `git remote -v` — list remotes75 - Check if local is ahead/behind remote76776. **Stale tracking branches**:78 - `git remote prune origin --dry-run` — branches that would be pruned79807. Present findings with recommendations818. If cleanup is recommended (gc, prune), show the commands but **always ask before running them**8283## Action: all84851. Find all git repositories under `~/Projects/`:86 - `find ~/Projects -maxdepth 3 -name .git -type d`872. For each repository found:88 a. Show repo name and path89 b. Run the **branches** check (merged + gone remotes)90 c. Show summary: N merged branches, M gone-remote branches913. **Per-repo confirmation** — ask before deleting branches in each repo924. Show overall summary at the end9394## Safety Rules95961. **Never delete without confirmation** — always preview first972. **Never delete protected branches** (main, master, develop)983. **Never `git push origin --delete` on protected branches**994. **Never run `git gc` without asking**1005. **Always preview first** — every action starts as a dry run1016. **Per-repo confirmation for `all`** — don't batch-delete across repos1027. Use `git branch -d` (safe delete) first; only offer `-D` with explicit warning