Skip to content

Add prune-merged-branches.py - #54

Merged
bonelifer merged 1 commit into
mainfrom
prune-merged-branches
Nov 7, 2026
Merged

bonelifer merged 1 commit into
mainfrom
prune-merged-branches

Conversation

@bonelifer

Copy link
Copy Markdown
Contributor

Adds prune-merged-branches.py, which deletes local branches whose work is already merged on GitHub, in one checkout or in every checkout inside a folder.

  • A branch that origin's default branch contains (a normal merge) is deleted with git branch -d.
  • A squash- or rebase-merged branch, which git branch -d refuses, is deleted with git branch -D only when a merged pull request's head commit is exactly the branch's tip. A branch that got commits after its PR was merged is kept.
  • The default branch, branches checked out in any worktree, wiki clones and checkouts whose origin isn't on GitHub are left alone.
  • --dry-run lists what would be deleted without deleting anything. Every kept branch is listed with the reason. With no arguments it prints help.

It reads origin's configured URL rather than git remote get-url, so a url.insteadOf rewrite doesn't hide that a checkout is on GitHub.

tests/test-prune-merged-branches.sh runs offline in CI: a local bare repository stands in for GitHub and a fake gh answers the default-branch and merged-PR questions. It covers a normal merge, a squash merge from the exact tip, a squash-merged branch with a later commit, an unmerged branch, a branch checked out in a worktree, the default branch, a wiki clone, a non-GitHub checkout, --dry-run, a single checkout as the path, and no arguments.

Testing (by Claude): the new tests pass and ShellCheck is clean. An earlier version deleted 19 merged branches across a folder of phpbbmodders checkouts and kept 38 with unmerged work; a dry run of this version on the same folder finds nothing left to delete and keeps the same 38.


Investigated and written by Claude on behalf of William Jacoby (bonelifer).

🤖 Generated with Claude Code

Deletes local branches whose work is already merged on GitHub, in one
checkout or a folder of them: git branch -d when main contains the
branch, and git branch -D for squash merges only when the merged PR's
head is exactly the branch's tip. Branches checked out in a worktree,
the default branch, wiki clones and non-GitHub checkouts are left alone.
tests/test-prune-merged-branches.sh covers it offline in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bonelifer
bonelifer merged commit 0acf70f into main Nov 7, 2026
1 check passed
@bonelifer
bonelifer deleted the prune-merged-branches branch November 7, 2026 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant