From c8ac759c10dbdef808e28be4ad0bb7339dd2e9e6 Mon Sep 17 00:00:00 2001 From: jmeridth Date: Thu, 12 Nov 2026 09:29:06 -0700 Subject: [PATCH] feat(repo-reset): force-delete verified squash-merged branches ## What/Why Let repo-reset clean up squash- and rebase-merged branches and their worktrees. Those used to get stuck because `git branch -d` refuses them and the skill banned `-D`. A branch now counts as merged only when a merged PR's head SHA matches its tip, not just its name, and force-deletes need a single batched [Y/n] confirmation. ## Proof it works - markdownlint and the pre-commit hooks pass. - bats tests not run: bats is not installed locally (they only cover reassess; repo-reset has none). - Ran the same verification by hand on a large repo: 8 squash-merged branches matched their merged PR heads exactly and were force-deleted cleanly. ## Risk Medium. The skill can now run `git branch -D`. The SHA match and the per-run confirmation gate it, and dirty worktrees are never touched. ## AI role Claude Opus 5.5 drafted the skill changes from Jason Meridth's direction. Jason set the design (worktrees follow their verified branch, one confirmation prompt) and reviews the change. ## Review focus The ancestor case in step 3 (tip behind the PR head) and the yes/no parsing in step 4. Signed-off-by: jmeridth --- jmeridth-skills/.claude-plugin/plugin.json | 2 +- jmeridth-skills/skills/repo-reset/SKILL.md | 88 ++++++++++++++-------- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/jmeridth-skills/.claude-plugin/plugin.json b/jmeridth-skills/.claude-plugin/plugin.json index dbe1116..143e40d 100644 --- a/jmeridth-skills/.claude-plugin/plugin.json +++ b/jmeridth-skills/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "jmeridth-skills", "description": "Personal collection of Claude Code skills for git workflow, GitHub Actions, Go development, code review, and productivity.", - "version": "0.1.23", + "version": "0.1.24", "author": { "name": "Jason Meridth", "email": "jmeridth@gmail.com", diff --git a/jmeridth-skills/skills/repo-reset/SKILL.md b/jmeridth-skills/skills/repo-reset/SKILL.md index c079a45..6f7a4d6 100644 --- a/jmeridth-skills/skills/repo-reset/SKILL.md +++ b/jmeridth-skills/skills/repo-reset/SKILL.md @@ -21,50 +21,78 @@ git checkout main git merge upstream/main --ff-only 2>/dev/null || git merge origin/main --ff-only ``` -- Prefer `upstream` remote if it exists, fall back to `origin` +- Prefer `upstream` remote if it exists, fall back to `origin`. Call the chosen remote `` below. - If the merge fails (not fast-forward), STOP and tell the user - do not force reset -### 3. Assess worktrees +### 3. Verify every branch -- List all worktrees: `git worktree list` (use `git worktree list --porcelain` for parsing) -- The main worktree (the repo path itself, now on `main`) is never touched - skip it -- For every other worktree, gather its state: - - **Branch**: the branch it has checked out (or "detached") - - **PR status**: `gh pr list --head --state merged --json number --jq '.[0].number'` (merged / open / none) - - **Dirty?**: `git -C status --porcelain` - non-empty means uncommitted changes or untracked files - - **Prunable?**: shown as `prunable` in `git worktree list --porcelain` -- Classify each worktree as one of: - - **Removable** - PR merged AND clean (no uncommitted changes) - - **Prunable** - the working directory is gone/stale - - **Skip (open PR)** - branch has an open PR - - **Skip (no PR)** - branch has no PR (likely local scratch work) - - **Skip (dirty)** - has uncommitted changes or untracked files +Check each local branch except `main` (and `master`), including branches checked out in worktrees. Branch state drives both worktree and branch cleanup, so verify it once here. -### 4. Clean up worktrees (before deleting branches) +- **Tip**: `git rev-parse ` +- **Worktree**: from `git worktree list --porcelain`, the path that has the branch checked out, if any. The main worktree (the repo path itself, now on `main`) is never touched. +- **Dirty?**: `git -C status --porcelain`. Non-empty output means uncommitted changes or untracked files. +- **Merged PR**: `gh pr list --head --state merged --json number,headRefOid,url`. A branch name alone does not prove anything. Someone else's fork, or an old PR from a reused branch name, can match. Pick the PR whose head matches the tip: + - The tip equals `headRefOid`, or + - The tip is an ancestor of `headRefOid`: `git fetch pull//head` then `git merge-base --is-ancestor FETCH_HEAD`. This covers a local branch that never pulled the PR's final commits. +- **Open PR**: `gh pr list --head --state open --json number --jq '.[0].number'` -- Remove each **Removable** worktree: `git worktree remove ` -- Prune **Prunable** worktrees: `git worktree prune` -- Do this BEFORE branch deletion: `git branch -d` refuses to delete a branch that is checked out in a worktree, so the worktree must go first -- **NEVER force-remove a worktree with uncommitted changes** (no `--force`) - report it as Skip (dirty) and leave it alone +Classify each branch: -### 5. Delete merged feature branches +- **Merged (fast-forward)**: the tip is an ancestor of `main` (`git merge-base --is-ancestor main`). `git branch -d` will accept it. +- **Merged (verified)**: not an ancestor of `main`, but a merged PR's head matches the tip as above. This is a squash or rebase merge. Deleting it needs `git branch -D`. +- **Skip (diverged)**: a merged PR exists for the branch name, but no merged PR's head matches the tip. The branch has commits that never merged. +- **Skip (open PR)**: the branch has an open PR. +- **Skip (no PR)**: no PR at all. This is likely local scratch work. -- List all local branches except `main` (and `master` if it exists) -- For each branch, check if its associated PR is merged using: `gh pr list --head --state merged --json number --jq '.[0].number'` -- If the PR is merged, delete the branch: `git branch -d ` -- If the branch has no PR or the PR is still open, skip it and report it -- **NEVER use `git branch -D` (force delete)** - only use `-d` (safe delete) +Then mark any merged branch whose worktree is dirty as **Skip (dirty)**. Uncommitted changes and untracked files are not in the PR. -### 6. Report summary +Prunable worktrees (the `prunable` marker in `git worktree list --porcelain`) are tracked separately. + +### 4. Confirm force-deletes (one prompt) + +If any branch is **Merged (verified)**, list them all in one batch and ask once: + +``` +These branches were squash/rebase merged. Their tips match the merged PR heads, so nothing is lost. +Force-delete them, along with their clean worktrees? + + [worktree: ] + ... + +Force-delete N verified branches? [Y/n] +``` + +- End the turn and wait for the reply. +- Treat `y`, `yes`, `confirmed`, `proceed`, or an empty reply as yes. Treat anything else as no. +- On no, keep those branches and their worktrees, and report them as "skipped (force-delete declined)". +- Skip this step entirely if there are no **Merged (verified)** branches. + +### 5. Clean up worktrees (before deleting branches) + +- Remove the worktree of each **Merged (fast-forward)** branch, and of each confirmed **Merged (verified)** branch: `git worktree remove ` +- Prune prunable worktrees: `git worktree prune` +- Do this BEFORE branch deletion. Git refuses to delete a branch that is checked out in a worktree. +- **NEVER force-remove a worktree** (no `--force`). A dirty worktree is already classified as Skip (dirty) and stays untouched. + +### 6. Delete merged branches + +- **Merged (fast-forward)**: `git branch -d ` +- **Merged (verified)** and confirmed in step 4: `git branch -D ` +- **Use `git branch -D` only on a branch that passed the SHA verification in step 3 AND was confirmed in step 4 of this run.** Never use it on any other branch. +- If `-d` unexpectedly refuses a fast-forward branch, do not escalate to `-D`. Report it as skipped. + +### 7. Report summary Print a summary: ``` Repo: Branch: main @ -Deleted branches: -Skipped branches: +Deleted branches: +Skipped branches: Worktrees removed: Worktrees pruned: -Worktrees skipped: +Worktrees skipped: ``` + +Link every PR in the summary as a clickable markdown link.