| name | ai-repo-health |
| description | Comprehensive repository health check with remote-first git hygiene, branch cleanup, and code quality analysis. Use for repo maintenance, or saying 'clean up the repo'. |
| argument-hint | [--remote-only] [--local-only] [--dry-run] |
Comprehensive repository health check with remote-first git hygiene: $ARGUMENTS
Deep repository analysis and cleanup that prioritizes remote repository hygiene, prevents lost work, and maintains code quality. Remote branches are cleaned first to ensure a truly clean repository state.
Usage Options
/ai-repo-health
/ai-repo-health --remote-only
/ai-repo-health --local-only
/ai-repo-health --issues-only
/ai-repo-health --code-only
/ai-repo-health --dry-run
/ai-repo-health --backup-first
/ai-repo-health --auto --max-age=90
Phase 1: Remote Repository Hygiene (Priority)
-
Sync and analyze remote state:
git fetch --all --prune --tags
git remote show origin
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
git for-each-ref --format='%(refname:short)|%(committerdate:relative)|%(authorname)|%(committername)' refs/remotes/origin/ | grep -v HEAD | sort -t'|' -k2 -r
-
Identify remote branches for cleanup:
git branch -r --merged origin/$DEFAULT_BRANCH | grep -v -E "(HEAD|$DEFAULT_BRANCH|master|main|develop|release|hotfix)"
for branch in $(git branch -r | grep -v HEAD); do
last_commit=$(git log -1 --format="%cr" $branch)
days_ago=$(git log -1 --format="%ct" $branch | xargs -I{} date -d @{} +%s | xargs -I{} echo $(( ($(date +%s) - {}) / 86400 )))
if [ $days_ago -gt 60 ]; then
echo "$branch|$last_commit|$days_ago days"
fi
done
-
Check GitHub PR status for each branch:
gh pr list --state all --head <branch-name> --json number,state,mergedAt
-
Interactive remote cleanup:
For each deletable remote branch:
Branch: origin/feature-xyz
Last commit: 3 months ago by John Doe
Status: PR #123 merged 3 months ago
Protected: No
Delete this remote branch? [y/n/skip-all/delete-all]
If confirmed:
git push origin --delete feature-xyz
-
Verify remote cleanup:
git fetch --prune
git branch -r | wc -l
Phase 2: Lost Work Recovery
-
Scan for orphaned commits:
git fsck --lost-found --no-reflogs | grep commit | cut -d' ' -f3 > /tmp/orphaned-commits.txt
while read commit; do
echo "=== Orphaned commit: $commit ==="
git show --stat --format="%h %ae %ad %s" --date=relative $commit
if git branch -r --contains $commit | grep -q .; then
echo "Exists on remote branch"
else
echo "WARNING: Not on any remote branch - AT RISK"
fi
done < /tmp/orphaned-commits.txt
-
Interactive recovery:
For each at-risk commit:
- Show full details and diff
- Options: Create recovery branch / Skip / Create stash
- If recovering:
git branch recovery/orphan-$SHORT_HASH $COMMIT
-
Stash management:
git stash list --format="%gd: %gs - %ar" | while read stash; do
age=$(echo $stash | grep -oE "[0-9]+ (weeks?|months?|years?) ago")
if [[ $age =~ (month|year) ]]; then
echo "Old stash: $stash"
fi
done
Phase 3: Local Branch Hygiene
-
Analyze local branches:
git branch -vv | grep ': gone]' | awk '{print $1}'
git branch --merged $DEFAULT_BRANCH | grep -v -E "^\*|$DEFAULT_BRANCH"
git branch -vv | grep -v '\[' | awk '{print $1}'
-
Interactive local cleanup:
For each category:
- Deleted remote: "Remote branch was deleted. Delete local?"
- Fully merged: "Branch is merged. Delete local?"
- No upstream: "No remote tracking. Push to remote or delete?"
Phase 4: Repository Optimization
-
Git maintenance:
du -sh .git
git reflog expire --expire=30.days --expire-unreachable=now --all
git gc --aggressive --prune=now
git repack -a -d -f --depth=250 --window=250
du -sh .git
-
Config cleanup:
for branch in $(git config --get-regexp '^branch\.' | cut -d. -f2 | sort -u); do
if ! git show-ref --verify --quiet refs/heads/$branch; then
echo "Removing config for deleted branch: $branch"
git config --remove-section branch.$branch 2>/dev/null
fi
done
Phase 5: Code & Project Health
-
GitHub issue management:
gh issue list --state open --json number,title,updatedAt --limit 100
-
Code quality scan:
rg -i "TODO|FIXME|HACK|XXX|BUG" --stats
find . -size +10M -type f | grep -v -E "\.git|node_modules|\.venv"
find . -name "*.pyc" -o -name "__pycache__" -o -name ".DS_Store" -o -name "*.log"
-
Dependency health:
if [ -f "pyproject.toml" ] || [ -f "requirements.txt" ]; then
pip-audit 2>/dev/null || uv run pip-audit 2>/dev/null
fi
if [ -f "package.json" ]; then
npm audit
npm outdated
fi
-
Documentation check:
rg '\[([^\]]+)\]\(([^)]+)\)' -o -r '$2' *.md | while read link; do
if [[ $link =~ ^[^:]+$ ]] && [ ! -e "$link" ]; then
echo "Broken link: $link"
fi
done
Phase 6: Comprehensive Report
- Generate detailed report:
# Repository Health Report - [DATE]
## Executive Summary
- Remote branches deleted: X
- Local branches deleted: Y
- Orphaned commits recovered: Z
- Repository size: X MB -> Y MB (Z% reduction)
## Remote Cleanup
### Deleted Remote Branches
- origin/feature-old (PR #123 merged 3 months ago)
### Kept Remote Branches
- origin/feature-active (recent activity)
## Local Cleanup
### Deleted Local Branches
- feature-old (tracking deleted remote)
### Branches Needing Attention
- feature-local-only (uncommitted changes)
## Recovered Work
### Created Recovery Branches
- recovery/orphan-abc123 (Fix critical bug)
## Code Health Issues
### High Priority
- 3 security vulnerabilities in dependencies
### Medium Priority
- 8 stale issues can be closed
## Recommendations
1. Review recovery branches within 7 days
2. Update dependencies to fix vulnerabilities
3. Enable branch protection rules
Safety Features
- Pre-cleanup backup:
git bundle create repo-backup-$(date +%Y%m%d).bundle --all
- Confirmation required for ALL remote deletions
- Never delete protected branches or branches with open PRs
- Recovery period: All deleted branches recoverable via reflog for 30 days
- Dry run mode:
--dry-run flag to preview without changes