git-branch-clean
Git Branch Clean
Sync remote state, then find and delete local branches whose remote is gone. Always confirm with user before any deletion.
Step-by-Step Process
Step 1: Fetch and prune remote refs
git fetch --prune
This removes remote-tracking refs that no longer exist on remote.
Step 2: Pull alive tracking branches
Get list of alive tracking branches (not gone, not current):
git branch -vv | grep -v ': gone]' | grep -v '^\*' | grep '\[origin/' | awk '{print $1}'
For each branch in that list, fast-forward update it locally (no checkout needed):
git fetch origin "<branch>:<branch>" 2>/dev/null && echo "Updated: <branch>" || echo "Skipped (diverged): <branch>"
Run one git fetch origin <branch>:<branch> per branch. If it fails (diverged), skip silently — don't block. Print result per branch so user can see progress.
Also pull current branch:
git pull --ff-only 2>/dev/null || echo "Current branch: skipped (diverged or no upstream)"
Step 3: Find stale branches (remote gone)
git branch -vv | grep ': gone]' | awk '{print $1}'
Step 4: Handle results
If no stale branches found:
"No stale branches. Local repo is clean."
Stop here.
If stale branches found, show list and ask user:
"Found [N] local branch(es) whose remote is gone:
[list each branch on its own line]
Delete all? (yes/no)"
Wait for user reply before continuing.
Step 5: Delete on confirm
If user says no → Stop. Tell user no branches were deleted.
If user says yes → Run:
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
Report each deleted branch and confirm done.
Error Handling
| Error | Action |
|---|---|
| Not inside a git repo | "Not a git repo. Run from project root." |
git fetch fails (no remote / auth error) |
Show error output. Stop. |
| Branch delete fails (e.g., currently checked out) | Show which branch failed, skip it, delete the rest |
| No branches at all | "Repo has no local branches to check." |
Safety Rules
- Never delete without user confirmation. Always show branch list first.
- Never force-delete current branch. If a gone branch is currently checked out, warn and skip it.
- Use
git branch -D(force delete) because gone branches have no upstream —-dwould refuse.
Example Output
Fetching and pruning... done.
Found 3 local branch(es) whose remote is gone:
feature/old-login
fix/AUTH-99-typo
chore/remove-deprecated-api
Delete all? (yes/no)
User types yes:
Deleted: feature/old-login
Deleted: fix/AUTH-99-typo
Deleted: chore/remove-deprecated-api
Done. 3 branch(es) removed.
More from nguyenhuy158/skills
odoo-code-review
Review Odoo (Python & XML) code for best practices, standards, and common errors. Use this skill when the user asks to review Odoo modules, Python files in an Odoo context, or XML views/data files.
45copilot-commit-style
Use this skill when the user asks to write, generate, draft, or format a git commit message. It enforces the Conventional Commits specification with mandatory emojis and strict formatting rules.
9pr-creator
Use this skill when user wants to create a pull request (PR) from current branch to another branch. Triggers on phrases like "create PR", "open PR", "make pull request", "tạo PR", "tạo pull request".
3code-polish
Polish Python code in git-changed files. Removes all comments and docstrings, eliminates magic strings and numbers, makes code self-documenting via naming. Python only — skips other languages. Use when user says "code-polish", "polish code", "clean code", "self-documenting", "remove comments", "làm sạch code".
2