git-cleanup
Safely analyzes and cleans up local git branches and worktrees by categorizing them as merged, squash-merged, superseded, or active work.
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- git-cleanup
- description
- Safely analyzes and cleans up local git branches and worktrees by categorizing them as merged, squash-merged, superseded, or active work.
- allowed-tools
- Bash(git branch:*), Bash(git worktree:*), Bash(git fetch:*), Bash(git log:*), Bash(git status:*), Bash(git symbolic-ref:*), Bash(git -C:*), Bash(*/git-kit/scripts/write-git-kit-marker.sh:*), Bash(*/git-kit/skills/git-cleanup/scripts/phase1-analysis.sh:*), Read, Grep
- disable-model-invocation
- true
02 资源文件 4
- 资产文件assets/analysis-report-template.md1.5 KB
- 参考文档references/critical-implementation-notes.md1.1 KB
- 参考文档references/testing-and-validation.md1.5 KB
- 脚本scripts/phase1-analysis.sh1.2 KB
03 SKILL.md 正文
Git Cleanup
Safely clean up accumulated git worktrees and local branches by categorizing them into: safely deletable (merged), potentially related (similar themes), and active work (keep).
On the Bash(git -C:*) grant: this is broad by necessity, not oversight — Phase 4's dirty-state and
gitignored-content checks run git -C <worktree-path> status --porcelain[--ignored] against a worktree
path that's only known at runtime, and Bash(prefix:*) permission syntax can't express "any -C <path>
but only followed by status" without a wrapper script. Reviewed 2026-08-11 (git-kit retro follow-up);
no narrower grant is expressible with the current permission syntax.
When to Use
- When the user has accumulated many local branches and worktrees
- When branches have been merged but not cleaned up locally
- When remote branches have been deleted but local tracking branches remain
When NOT to Use
- Do not use for remote branch management (this is local cleanup only)
- Do not use for repository maintenance tasks like gc or prune
- Not designed for headless or non-interactive automation (requires user confirmations at two gates)
- Cannot run from a session sandboxed to a worktree checkout — this command needs to operate on the
primary checkout's branch/worktree list, and a worktree-sandboxed session cannot
cdthere. Run it from a session started in the primary checkout instead. Seefinishing-work's hand-off note for the same constraint on the more common path into this skill.
Core Principle: SAFETY FIRST
Never delete anything without explicit user confirmation. This skill uses a gated workflow where users must approve each step before any destructive action.
Critical Implementation Notes
See references/critical-implementation-notes.md for the
squash-merge -D requirement and the PR-history investigation commands — the branch-grouping procedure
itself is already fully covered by Phase 2 below, so the reference file doesn't restate it. Read the
reference before Phase 3 (Categorize) for the squash-merge nuance; the PR-history commands apply
specifically to [gone] branches within Phase 2/3.
Workflow
Phase 1: Comprehensive Analysis
Gather ALL information upfront before any categorization. Run "${CLAUDE_PLUGIN_ROOT}/skills/git-cleanup/scripts/phase1-analysis.sh" directly via Bash — it resolves the default branch, lists local branches and worktrees, fetches/prunes, gets merged-branch and recent PR-merge history, and for each non-protected branch (protected names excluded via the script's own grep -vE filter) reports unmerged and unpushed commits. Read its output rather than re-deriving these git calls by hand.
Note on branch names: Git branch names can contain characters that break shell expansion. Always quote "$branch" in commands.
Phase 2: Group Related Branches
Do this BEFORE individual categorization.
Identify branch groups by shared prefixes:
# List branches and extract prefixes
git branch --format='%(refname:short)' | sed 's/-[^-]*$//' | sort | uniq -c | sort -rn
For each group with 2+ branches:
- Compare commit histories - Which branches contain commits from others?
- Find merge evidence - Which PRs incorporated work from this group?
- Identify the "final" branch - Usually the most recent or most complete
- Mark superseded branches - Older iterations whose work is in main or in a newer branch
SUPERSEDED requires evidence, not just shared prefix:
- A PR merged the work into main, OR
- A newer branch contains all commits from the older branch
- Name prefix alone is NOT sufficient — similarly named branches may contain independent work
Example analysis for feat/api-* branches:
### Related Branch Group: feat/api-*
| Branch | Commits | PR Merged | Status |
|--------|---------|-----------|--------|
| feat/api | 12 | #29 (initial API) | Superseded - work in main |
| feat/api-v2 | 8 | #45 (API improvements) | Superseded - work in main |
| feat/api-refactor | 5 | #67 (refactor) | Superseded - work in main |
| feat/api-final | 4 | None found | Superseded by above PRs |
**Recommendation:** All 4 branches can be deleted - work incorporated via PRs #29, #45, #67
Phase 3: Categorize Remaining Branches
For branches NOT in a related group, categorize individually:
Is branch merged into default branch?
├─ YES → SAFE_TO_DELETE (use -d)
└─ NO → Is tracking a remote?
├─ YES → Remote deleted? ([gone])
│ ├─ YES → Was work squash-merged? (check main for PR)
│ │ ├─ YES → SQUASH_MERGED (use -D)
│ │ └─ NO → REMOTE_GONE (needs review)
│ └─ NO → Local ahead of remote? (check: git log origin/<branch>..<branch>)
│ ├─ YES (has output) → UNPUSHED_WORK (keep)
│ └─ NO (empty output) → SYNCED_WITH_REMOTE (keep)
└─ NO → Has unique commits?
├─ YES → LOCAL_WORK (keep)
└─ NO → SAFE_TO_DELETE (use -d)
Category definitions:
| Category | Meaning | Delete Command |
|---|---|---|
| SAFE_TO_DELETE | Merged into default branch | git branch -d |
| SQUASH_MERGED | Work incorporated via squash merge | git branch -D |
| SUPERSEDED | Part of a group, work verified in main via PR or in newer branch | git branch -D |
| REMOTE_GONE | Remote deleted, work NOT found in main | Review needed |
| UNPUSHED_WORK | Has commits not pushed to remote | Keep |
| LOCAL_WORK | Untracked branch with unique commits | Keep |
| SYNCED_WITH_REMOTE | Up to date with remote | Keep |
Phase 4: Dirty State Detection
Check ALL worktrees and current directory for uncommitted changes:
# For each worktree path
git -C <worktree-path> status --porcelain
# For current directory
git status --porcelain
Also check for gitignored content — git status --porcelain never lists gitignored files, tracked
or not, so a worktree can carry a .env, a local settings override, or other gitignored content that this
check alone would silently miss:
# For each worktree path
git -C <worktree-path> status --porcelain --ignored
# For current directory
git status --porcelain --ignored
Do not use git clean here — it's a destructive command with no place in an analysis-only phase of a
skill whose Core Principle is "never delete anything without explicit user confirmation." --ignored is
read-only and sufficient. If the ignored-file list is long (e.g. node_modules/, .venv/), show only the
top-level entries plus a count rather than every path, so the warning stays readable.
Exclude .claude/settings.local.json and any **/CLAUDE.local.md from this warning — starting-work
now copies both into every worktree it creates from the main worktree's own copy, so a worktree's version
is a duplicate, not the only copy; deleting the worktree does not lose them the way an untracked .env or
scratch file would. Do not list these two patterns under "will also be deleted" below.
Display warnings prominently:
WARNING: ../proj-auth has uncommitted changes:
M src/auth.js
?? new-file.txt
Ignored (not tracked by git, will also be deleted):
.env
These changes will be LOST if you remove this worktree.
Check for session locks: git worktree list --verbose shows locked: <reason> for any locked
worktree (starting-work locks every worktree it creates — see its own Instructions). A locked worktree
recommended for deletion needs an unlock step before git worktree remove — note this against that
worktree in the analysis rather than only discovering it when the removal command itself fails.
GATE 1: Present Complete Analysis
Present everything in ONE comprehensive view. Group related branches together. See assets/analysis-report-template.md for the full example layout (related branch groups, individual branch categories, worktrees, and summary).
Use AskUserQuestion with clear options:
- Delete all recommended (groups + merged + squash-merged)
- Delete specific groups/categories
- Let me pick individual branches
Do not proceed until user responds.
GATE 2: Final Confirmation with Exact Commands
Show the EXACT commands that will run, with correct flags:
I will execute:
# Merged branches (safe delete)
git branch -d fix/typo
# Squash-merged branches (force delete - work is in main via PRs)
git branch -D feat/login
git branch -D feat/api
git branch -D feat/api-v2
git branch -D feat/api-refactor
git branch -D feat/api-final
# Worktrees (unlock first if session-locked, per Phase 4's lock check)
git worktree unlock ../proj-auth
git worktree remove ../proj-auth
Confirm? (yes/no)
IMPORTANT: This is the ONLY confirmation needed for deletion. Do not add extra confirmations if -D is required.
Phase 5: Execute
Run each deletion as a separate command so partial failures don't block remaining deletions. Report the result of each. Immediately before each git branch -D call — never earlier, since git-kit's guard hook only accepts a marker up to 60 seconds old — run "${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup. This writes the marker git-kit's destructive-cleanup guard requires before it will let a raw git branch -D targeting a protected branch name through — the same marker-handshake pattern every other git-kit skill uses before its own guarded command. Plain git branch -d (lowercase, already-merged-only) and plain git worktree remove (no --force) aren't guarded and need no marker; only git worktree remove --force/-f does, since a plain removal already refuses on a dirty or locked worktree via git's own safeguard.
git branch -d fix/typo
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git branch -D feat/login
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git branch -D feat/api
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git branch -D feat/api-v2
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git branch -D feat/api-refactor
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git branch -D feat/api-final
git worktree unlock ../proj-auth
"${CLAUDE_PLUGIN_ROOT}/scripts/write-git-kit-marker.sh" git-cleanup-destructive git-cleanup
git worktree remove ../proj-auth
If a deletion fails, report the error and continue with remaining deletions. If git worktree remove
fails specifically because the worktree is still locked (the unlock step above was skipped, or a lock was
added after Phase 4's check), report this distinctly from a generic removal failure — say plainly that it's
locked and by which reason, rather than surfacing git's raw error text unexplained.
Phase 6: Report
## Cleanup Complete
### Deleted
- fix/typo
- feat/login
- feat/api
- feat/api-v2
- feat/api-refactor
- feat/api-final
- Worktree: ../proj-auth
### Remaining (4 branches)
| Branch | Status |
|--------|--------|
| main | current |
| wip/new-feature | active work |
| experiment/old | needs review |
Safety Rules
- Never invoke automatically - Only run when user explicitly uses
/git-cleanup - Two confirmation gates only - Analysis review, then deletion confirmation
- Use correct delete command -
-dfor merged,-Dfor squash-merged/superseded - Never touch protected branches - main, master, develop, release/* are excluded from Phase 1's
per-branch commit-analysis loop by
scripts/phase1-analysis.sh's owngrep -vEfilter (run directly, not just read), and any rawgit branch -Dthat still targets one of these names is additionally hard-blocked by git-kit'sguard-raw-destructive-cleanup.shPreToolUse hook - Block dirty worktree removal - Refuse without explicit data loss acknowledgment. "Dirty" covers
gitignored content too, not just tracked/untracked-but-not-ignored changes —
git status --porcelainalone misses gitignored files entirely, so Phase 4's--ignoredcheck is what actually completes this rule's coverage - Unlock before removing - A session-locked worktree (per Phase 4's lock check) gets
git worktree unlockimmediately beforegit worktree remove, never--forceas a substitute for unlocking —--forcebypasses the dirty-worktree safeguard in Safety Rule 5 too, not just the lock - Group related branches - Don't scatter them across categories
Rationalizations to Reject
These are common shortcuts that lead to data loss. Reject them:
| Rationalization | Why It's Wrong |
|---|---|
| "The branch is old, it's probably safe to delete" | Age doesn't indicate merge status. Old branches may contain unmerged work. |
| "I can recover from reflog if needed" | Reflog entries expire. Users often don't know how to use reflog. Don't rely on it as a safety net. |
| "It's just a local branch, nothing important" | Local branches may contain the only copy of work not pushed anywhere. |
| "The PR was merged, so the branch is safe" | Squash merges don't preserve branch history. Verify the specific commits were incorporated. |
"I'll just delete all the [gone] branches" |
[gone] only means the remote was deleted. The local branch may have unpushed commits. |
| "The user seems to want everything deleted" | Always present analysis first. Let the user choose what to delete. |
| "The branch has commits not in main, so it has unpushed work" | "Not in main" ≠ "not pushed". A branch can be synced with its remote but not merged to main. Always check git log origin/<branch>..<branch>. |
| "It's gitignored, so it's not important" | Gitignored means not in git history — it is often the only copy of a .env, a local override, or uncommitted scratch output. Absence from git is not evidence of unimportance. |
Testing & Validation
See references/testing-and-validation.md for activation triggers/non-triggers, the Phase 4 gitignored-content detection scenarios, and the full quality-gates checklist.
04 同仓库更多 Skills
demo
Fixture skill used by scripts.marketplace_ci sync tests.
export-demo
Fixture skill used by scripts.marketplace_ci export tests.
work-to-development
Validate an accepted Linear Issue's readiness for implementation (criteria, dependencies, duplicate/existing artifacts) and request governed branch/worktree creation through git-kit:starting-work — recording work-started Git/GitHub evidence and a deliberate Linear Started transition only after Git identity is read back and verified. Use when asked to start work on a Linear issue, begin development for an accepted issue, or prepare an accepted issue for implementation. Never creates a branch directly — always delegates to git-kit.
work-linking
Create and maintain stable cross-system links and authority labels between Notion and Linear records, and detect/repair drift between them. Use when asked to link a Notion record to a Linear record, check for drift between Notion and Linear, or repair a broken/stale link. Repairs only the bounded non-authoritative copy or link — never chooses by newest timestamp, and never creates a reverse-write loop against either system's own authority.
status-and-learning
Produce a dated, explicitly non-live Linear-to-Notion progress summary, or capture an outcome/learning after work completes. Use when asked to summarize progress to Notion, report portfolio status, or capture what was learned from a completed piece of work — not to disposition open follow-up items (see open-item-management for that). Never runs automatically or continuously — every summary is a deliberate, approved snapshot, not a live sync.
repository-gates
Discover, delegate, record, and invalidate this repository's actual Git/GitHub lifecycle gates (pre-commit, pre-push, PR required checks, Codex delta review, review requirements, merge rights) and the repository-policy provider profile that maps governed operations to git-kit. Use when asked what gates apply to this repository, to discover repository policy before starting Wave 2 work, or to check whether prior gate evidence is still valid after new commits. Discovery and delegation only — never executes a gate's own internal logic, and never invents a gate the repository doesn't actually define (in particular, never assumes a "Review Changes" gate exists).