You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit a69a60b
Browse filesBrowse the repository at this point in the historyBrowse files
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]>
Copy file name to clipboardExpand all lines: .claude/skills/post-merge-cleanup/SKILL.md
+40-7Lines changed: 40 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
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.
4
4
allowed-tools: Bash, ExitWorktree
5
5
argument-hint: "[branch]"
6
6
---
7
7
8
8
# Post-Merge Cleanup
9
9
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.
11
11
12
12
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.
13
13
@@ -30,7 +30,7 @@ git worktree list --porcelain # which checkou
30
30
gh pr list --state merged --head "$BRANCH" --json number,title,headRefOid,mergedAt
31
31
git rev-parse "$BRANCH"
32
32
```
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.
34
34
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.
35
35
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`.
36
36
@@ -61,24 +61,57 @@ Skip this step if there is no `BRANCH`.
61
61
62
62
```bash
63
63
git -C "$MAIN" branch -D "$BRANCH"# skip if ExitWorktree already deleted it
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.
69
69
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:
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
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
71
103
72
104
```bash
73
105
cd"$MAIN"&& npm install
74
106
```
75
107
76
-
## 7. Report
108
+
## 8. Report
77
109
78
110
Keep the report short:
79
111
80
112
- the worktree removed, if any
81
113
- 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
82
115
- the `master` commit you are now on (`git log --oneline -1`)
83
116
- whether `npm install` succeeded
84
117
-`git status --short`. Call out `package-lock.json` if `npm install` changed it.
0 commit comments