| name | git-branch-cleanup |
| description | Analyzes and safely cleans up local Git branches. Categorizes branches by merge status,
staleness, and remote tracking. Provides interactive selection with safety guards.
Use when the user wants to clean up branches, delete old branches, organize Git branches,
or asks about which branches can be safely deleted.
|
| compatibility | Requires git 2.17+ (for worktree support). Git 2.22+ recommended for `git branch --show-current`. |
Git Branch Cleanup
Safely organize and clean up local Git branches. Categorizes branches by merge status, staleness, and remote tracking, then guides users through safe deletion.
Contents
- Quick Start
- Workflow (Steps 1-5)
- Commands Reference
- Dry Run Mode
- Safety Checklist
Quick Start
- Analyze branches (Step 1)
- Categorize by safety level (Step 2)
- Display results and ask user which to delete (Step 3)
- Verify against safety guards (Step 4)
- Execute deletion after confirmation (Step 5)
Workflow
Step 1: Branch Analysis
Collect information with these commands:
BASE_BRANCH="$(
git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@'
)"
[ -n "$BASE_BRANCH" ] || BASE_BRANCH=main
git for-each-ref --sort=-committerdate refs/heads/ \
--format='%(refname:short)|%(committerdate:relative)|%(upstream:trackshort)|%(contents:subject)'
git branch --merged "$BASE_BRANCH"
git branch --no-merged "$BASE_BRANCH"
git branch -vv | grep -F ': gone]' || true
git worktree list
git branch -v | grep '^+' || true
git for-each-ref refs/heads \
--format='%(refname:short) %(upstream:trackshort)' \
| grep '>' || true
Step 2: Categorization
Categorize branches by safety level:
| Category | Description | Safety |
|---|
| Merged (Safe) | Already merged to base branch | Safe to delete |
| Gone Remote | Remote branch deleted | Review recommended |
| Stale | No commits for 30+ days | Needs review |
| Has Worktree | Branch checked out in a worktree (+ prefix in git branch -v) | Remove worktree first |
| Ahead of Upstream | Has unpushed commits | ⚠️ Do not delete |
| Unmerged | Active work in progress | Use caution |
Note: 30 days is a reasonable default for stale detection. Adjust based on team's sprint cycle if needed (e.g., 14 days for 2-week sprints).
Step 3: Display Format
## Branch Analysis Results
### Safe to Delete (Merged)
- feature/login (3 weeks ago) - Add login feature
- fix/typo (2 months ago) - Fix typo in readme
### Remote Deleted
- feature/old-api (1 month ago) - Remote branch no longer exists
### ⚠️ Ahead of Upstream (DO NOT DELETE)
- feature/wip (1 day ago) - Has 3 unpushed commits
### Stale Branches (30+ days)
- experiment/cache (2 months ago) - Unmerged
### Active (Unmerged)
- feature/new-dashboard (2 days ago) - Work in progress
---
Which categories would you like to delete?
1. Merged only (safest)
2. Merged + remote deleted
3. Select individually
Step 4: Safety Guards
Never delete these branches:
main
master
trunk
develop
development
- Currently checked out branch
Always confirm before deletion:
The following branches will be deleted:
- feature/login
- fix/typo
Continue? (y/N)
Step 5: Execute Deletion
git branch -d <branch-name>
git branch -D <branch-name>
For branches with worktrees, remove the worktree first:
git worktree list | grep "\\[$branch\\]"
git worktree remove --force /path/to/worktree
git branch -D <branch-name>
Bulk delete [gone] branches with worktree handling:
git branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do
echo "Processing branch: $branch"
worktree=$(git worktree list | grep "\\[$branch\\]" | awk '{print $1}')
if [ ! -z "$worktree" ] && [ "$worktree" != "$(git rev-parse --show-toplevel)" ]; then
echo " Removing worktree: $worktree"
git worktree remove --force "$worktree"
fi
echo " Deleting branch: $branch"
git branch -D "$branch"
done
Commands Reference
BASE_BRANCH="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@')"
[ -n "$BASE_BRANCH" ] || BASE_BRANCH=main
CURRENT_BRANCH="$(git branch --show-current)"
for branch in $(git branch --merged "$BASE_BRANCH" | sed 's/^[* ]*//'); do
case "$branch" in
main|master|trunk|develop|development|"$CURRENT_BRANCH"|"") continue ;;
*) git branch -d "$branch" ;;
esac
done
git fetch --prune || echo "Warning: Could not reach remote. Using cached data."
git for-each-ref --sort=committerdate refs/heads/ \
--format='%(committerdate:short) %(refname:short)' | head -20
git worktree list
git worktree remove /path/to/worktree
git worktree remove --force /path/to/worktree
git worktree prune
Dry Run Mode
Preview deletions without executing:
BASE_BRANCH="$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@')"
[ -n "$BASE_BRANCH" ] || BASE_BRANCH=main
CURRENT_BRANCH="$(git branch --show-current)"
for branch in $(git branch --merged "$BASE_BRANCH" | sed 's/^[* ]*//'); do
case "$branch" in
main|master|trunk|develop|development|"$CURRENT_BRANCH"|"") ;;
*) echo "$branch" ;;
esac
done
Trigger dry-run mode if user says "--dry-run", "preview", or "just show me".
Safety Checklist
Before deletion, verify:
Quick safety commands
git for-each-ref refs/heads \
--format='%(refname:short)|%(upstream:short)|%(upstream:trackshort)|%(committerdate:relative)|%(contents:subject)' \
--sort=-committerdate
git log --oneline @{u}..HEAD 2>/dev/null || echo "No upstream configured for this branch"
git branch -v | grep "^+" | awk '{print $2}'
git worktree list | grep "\\[branch-name\\]"