cleaning-up-merged-branches
Skill41 starsChanged 42 days agoMITView on GitHub
Syncs main and deletes the local feature branch after a PR is merged on GitHub. Use when the user says they merged or squash-merged a PR ("I merged PR 411 on GitHub", "merged the PR", "clean up the branch"). Codifies this repo's squash-merge default, where `git branch -d` wrongly reports "not fully merged" and a verified force-delete is required.
- Commits and pushes
In this repository33 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- branch-preflightSkill
- cleaning-up-merged-branchesSkill
- diagnoseSkill
- grill-meSkill
- grill-with-docsSkill
- handoffSkill
- improve-codebase-architectureSkill
- inkwell-retrofitSkill
- open-knowledgeSkill
- pb-collectionSkill
- reviewable-pr-slicingSkill
- setup-matt-pocock-skillsSkill
- shadcnSkill
- shipSkill
- source-command-build-nextSkill
- source-command-qcheckSkill
- source-command-triage-prsSkill
- to-issuesSkill
- verify-frontend-changeSkill
- branch-preflightSkill
- claude-code-primitivesSkill
- inkwell-retrofitSkill
- pb-collectionSkill
- reviewable-pr-slicingSkill
- verify-frontend-changeSkill
- verify-gsdSkill
- verify-remindersSkill
- verify-sync-convergenceSkill
- verify-sync-sessionSkill
- verify-task-tombstonesSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 40
Only when step 4 came back non-empty, ask GitHub whether the branch was merged.
- Very specific: line 52
Stop, show the user git log --oneline <headRefOid>..<branch>, and do NOT force-delete.
- Very specific: line 54
No PR, or only OPEN/CLOSED ones → genuinely unmerged.
≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Cleaning up merged branches
Sync main and remove the now-stale local feature branch after the user merges a
PR on GitHub. This repo squash-merges PRs by default, so the plain git branch -d
refuses to delete the branch. Verify the content actually landed in main, then
force-delete.
Verification is two-tier on purpose. The cheap local check (step 4) is conclusive
only for a branch cut from the current main; it decays into false alarms as the
branch ages. When it comes back dirty, step 5 asks GitHub, which is authoritative
regardless of how the merge rewrote history. Never force-delete on a [gone]
upstream marker alone: that only means the remote branch was deleted, which says
nothing about whether the work merged.
Steps
- Fetch, prune stale remote-tracking refs, and note the current branch:
git fetch origin --prune && git branch --show-current - Switch to
mainand pull the merge (localmainstill points at the pre-merge commit until you do this):git checkout main && git pull origin main - Try the safe delete of the feature branch:
git branch -d <branch>- Succeeds → done; go to Verify.
- Fails with
error: the branch '<branch>' is not fully merged→ expected for a squash merge. GitHub squashed the branch into one new commit, so its original commits are not ancestors ofmaineven though the content landed. Continue.
- Confirm the branch's content is fully in
mainbefore force-deleting. The':!.blume'pathspec excludes local.blume/tool artifacts that are never committed:git diff main <branch> -- . ':!.blume'- Empty output → content is fully in
main; safe to force-delete. - Non-empty output → this check has expired, not failed. It only proves a
merge for a branch cut from the current
main. Fall through to step 5.
- Empty output → content is fully in
- Only when step 4 came back non-empty, ask GitHub whether the branch was merged.
This survives any history rewrite, and still answers after the remote branch is
deleted.
--headmatches on branch name, so you must also confirm the PR was built from the commit the local branch actually points at:gh pr list --state all --head <branch> --json number,state,mergedAt,headRefOid git rev-parse <branch>- A
MERGEDPR whoseheadRefOidequals the local tip → the content landed; safe to force-delete. - A
MERGEDPR whoseheadRefOiddiffers → the local branch has moved since that PR merged, or the name was reused. Those extra commits are not inmain. Stop, show the usergit log --oneline <headRefOid>..<branch>, and do NOT force-delete. - No PR, or only
OPEN/CLOSEDones → genuinely unmerged. Stop, show the usergit cherry -v main <branch>, and do NOT force-delete.
- A
- Force-delete the redundant local branch:
git branch -D <branch>
Verify
git branchshows the feature branch is gone and you are onmain.git log --oneline -1showsmainat the squash-merge commit.
Gotchas
git diff main <branch>is bidirectional, so it expires. It reports work that landed inmainafter the branch was cut as though the branch removed it. That is harmless right after a merge, which is the case this skill was written for, but a branch left sitting for a few weeks reports thousands of diff lines even when its content merged cleanly. A non-empty diff on an older branch is not evidence of unmerged work; it is the check aging out. That is what step 5 is for.git cherry main <branch>is not a fix for that. It compares patch-ids, and a squash-merge collapses N commits into one that matches none of the N originals. The tell is a perfect correlation between commit count and verdict: every single-commit branch reads "already upstream" and every multi-commit branch reads "not in main". That pattern means squash-merge, not lost work. It is still useful as evidence for deletion (nothing flagged → definitely merged), never against.- A merged PR proves the name merged, not the branch you are holding.
gh pr list --headfilters by branch name, so a branch that gained local commits after its PR merged, or a name reused for new work, still returns the oldMERGEDrecord. Acting on that deletes commits that never landed. This is why step 5 comparesheadRefOidagainstgit rev-parse <branch>rather than trusting theMERGEDstate alone.headRefOidremains readable long after the remote branch is gone, which is what makes the comparison possible at cleanup time. git worktree remove --forcediscards uncommitted work silently. Rungit -C <worktree> status --shortfirst and show the user anything it finds.- Deleting multiple branches at once: zsh does not word-split an unquoted
variable (unlike bash), so
git branch -D $branchespasses the whole list as one bad refspec and deletes nothing. Pipe the list throughxargsinstead:... | xargs git branch -D. - Deleting remote branches (
git push origin --delete <branch>) is externally visible — confirm with the user first. Local cleanup above does not require it;git fetch origin --prunealready drops stale remote-tracking refs.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

