Skip to content

Commit a69a60b

Browse files
dolanmiuclaude
andauthored
chore: sweep other merged branches in post-merge cleanup (#3873)
* chore: sweep other merged branches in post-merge cleanup Branches from earlier PRs pile up whenever a cleanup is skipped. The skill now checks every other local and origin branch, finds the PRs that contain its tip by commit, and deletes it only when a merged PR has that tip as its head or in its history and no open PR uses it. next-major/*, gh-pages and branches in worktrees are left alone, and anything without a merged PR is kept and reported. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * chore: delete a branch on origin only while it is at the tip that was checked A plain `push --delete` removes whatever the branch points to when the push runs, so commits pushed after the check would go unchecked. Both remote deletes now lease on the checked tip: the PR's head in step 5, and the origin tip in step 6. Git refuses the delete with "stale info" if the branch has moved. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 19087e8 commit a69a60b

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

  • .claude/skills/post-merge-cleanup

‎.claude/skills/post-merge-cleanup/SKILL.md‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
name: post-merge-cleanup
3-
description: Reset the repo after a PR has merged, ready for the next piece of work. Removes the branch's worktree if it has one, goes back to master, pulls, deletes the merged branch locally and on origin, and runs npm install. Use when the user says their PR is merged and wants to clean up or start the next task.
3+
description: Reset the repo after a PR has merged, ready for the next piece of work. Removes the branch's worktree if it has one, goes back to master, pulls, deletes the merged branch locally and on origin, deletes any other branches whose PRs have merged, and runs npm install. Use when the user says their PR is merged and wants to clean up or start the next task, or wants old merged branches cleared out.
44
allowed-tools: Bash, ExitWorktree
55
argument-hint: "[branch]"
66
---
77

88
# Post-Merge Cleanup
99

10-
Bring the repo back to a clean, up-to-date `master` after a PR has merged. The branch to clean up is `$ARGUMENTS` if given, otherwise the branch checked out in the current directory.
10+
Bring the repo back to a clean, up-to-date `master` after a PR has merged. The branch to clean up is `$ARGUMENTS` if given, otherwise the branch checked out in the current directory. Branches from earlier PRs are swept up too, because they pile up whenever a cleanup is skipped.
1111

1212
PRs here are squash merged, so the branch's commits never appear on `master` and `git branch -d` always refuses. Merge status therefore comes from GitHub, and the safety checks below are what make it safe to force-delete. If any check fails, stop and tell the user what you found. Never force anything past a failed check.
1313

@@ -30,7 +30,7 @@ git worktree list --porcelain # which checkou
3030
gh pr list --state merged --head "$BRANCH" --json number,title,headRefOid,mergedAt
3131
git rev-parse "$BRANCH"
3232
```
33-
Stop if no merged PR is listed. Stop if the local tip differs from the PR's `headRefOid`, because it has commits that never reached the PR.
33+
Stop if no merged PR is listed. Stop if the local tip differs from the PR's `headRefOid` (`PR_HEAD`), because it has commits that never reached the PR.
3434
2. **No uncommitted work in the worktree** (only when `WORKTREE` exists). `git -C "$WORKTREE" status --porcelain` must be empty. Removing the worktree would delete anything listed, including untracked files.
3535
3. **No tracked changes in `MAIN`.** `git -C "$MAIN" status --porcelain --untracked-files=no` must be empty. Untracked files in `MAIN`, such as plan notes, are fine because they carry over to `master`.
3636

@@ -61,24 +61,57 @@ Skip this step if there is no `BRANCH`.
6161

6262
```bash
6363
git -C "$MAIN" branch -D "$BRANCH" # skip if ExitWorktree already deleted it
64-
git -C "$MAIN" ls-remote --exit-code --heads origin "$BRANCH" && git -C "$MAIN" push origin --delete "$BRANCH"
64+
git -C "$MAIN" ls-remote --exit-code --heads origin "$BRANCH" && git -C "$MAIN" push origin --delete --force-with-lease="$BRANCH:$PR_HEAD" "$BRANCH"
6565
git -C "$MAIN" fetch --prune
6666
```
6767

68-
GitHub often deletes the remote branch on merge. `ls-remote` exits non-zero when it is already gone, and then there is nothing to push.
68+
GitHub often deletes the remote branch on merge. `ls-remote` exits non-zero when it is already gone, and then there is nothing to push. The lease makes the delete fail with `stale info` if someone pushed to the branch after the PR merged. Then stop and report it.
6969

70-
## 6. Reinstall dependencies
70+
## 6. Delete other merged branches
71+
72+
List every other branch, locally and on origin, and the branches checked out in worktrees:
73+
74+
```bash
75+
git -C "$MAIN" for-each-ref --format='%(refname:short) %(objectname)' refs/heads
76+
git -C "$MAIN" for-each-ref --format='%(refname:lstrip=3) %(objectname)' refs/remotes/origin
77+
git -C "$MAIN" worktree list --porcelain
78+
```
79+
80+
Leave these alone without checking them: `master`, `gh-pages`, `HEAD`, `next-major/*` (breaking changes saved for the next major, which have no PR), and any branch checked out in a worktree.
81+
82+
Check each remaining branch `B`. A branch that is both local and on origin is checked once per tip, because the two can differ. Find the PRs that contain its tip `TIP`. The lookup goes by commit, so it also finds the PR for a branch whose name differs from the PR's head, such as `pr-3768` fetched from a fork PR or a local copy of a Dependabot branch:
83+
84+
```bash
85+
gh api "repos/{owner}/{repo}/commits/$TIP/pulls" --jq '.[] | select(.merged_at) | "\(.number) \(.head.sha)"'
86+
gh pr list --state open --head "$B" --json number
87+
```
88+
89+
Delete `B` only when a merged PR is listed, no open PR has `B` as its head, and `TIP` is the merged PR's head or an earlier commit of it. The earlier-commit case comes up when the PR gained commits after the branch was copied:
90+
91+
```bash
92+
git -C "$MAIN" fetch -q origin "pull/$PR/head" # only when TIP is not the PR's head
93+
git -C "$MAIN" merge-base --is-ancestor "$TIP" FETCH_HEAD
94+
git -C "$MAIN" branch -D "$B" # local tip
95+
git -C "$MAIN" push origin --delete --force-with-lease="$B:$TIP" "$B" # tip on origin
96+
```
97+
98+
The lease makes the delete fail with `stale info` if the branch on origin has moved since its tip was checked. Keep that branch and report it.
99+
100+
Keep every other branch and list it in the report with its last commit date and subject. It may hold work that never reached a PR, so delete it only when the user says to.
101+
102+
## 7. Reinstall dependencies
71103

72104
```bash
73105
cd "$MAIN" && npm install
74106
```
75107

76-
## 7. Report
108+
## 8. Report
77109

78110
Keep the report short:
79111

80112
- the worktree removed, if any
81113
- the branch deleted, both locally and on origin
114+
- the other merged branches deleted in step 6, local and on origin, and the branches kept, with the reason for each
82115
- the `master` commit you are now on (`git log --oneline -1`)
83116
- whether `npm install` succeeded
84117
- `git status --short`. Call out `package-lock.json` if `npm install` changed it.

0 commit comments

Comments
 (0)