feat(scripts): cross-repo GitHub branch hygiene defaults#46
Merged
Conversation
Adds init-repo-github-defaults.{sh,ps1} -- a small idempotent tool that
enables `delete_branch_on_merge: true` on any GitHub repo via gh API,
plus a one-time PATCH for repos that drifted before this script existed.
Why
---
2026-05-18 marathon session: PRs #38-#45 merged via a mix of gh CLI and
the GitHub web UI. The CLI invocations did not include --delete-branch
on every call; the web UI honoured the repo's setting, which was
disabled. Result: 8 orphan branches on origin after the session, all
stale (their content was already squashed into main).
The retro-cleanup took 30 seconds (loop of `git push origin --delete`),
but the policy fix prevents recurrence permanently -- one PATCH per repo
and GitHub auto-deletes the head branch on every future merge.
What
----
- scripts/init-repo-github-defaults.sh -- bash, idempotent, derives
owner/name from origin remote
(handles ssh + https forms),
--dry-run, --help, --repo
override, --repo for cross-
repo backfill
- scripts/init-repo-github-defaults.ps1 -- PowerShell parity, ASCII-only
per project convention
(PSScriptAnalyzer rule
PSUseBOMForUnicodeEncodedFile),
-DryRun switch
- tests/init-repo-github-defaults.bats -- 17 assertions: syntax, idem-
potence early-exit, --dry-run,
--help, unknown-arg
rejection, ASCII-only on .ps1,
parity between .sh and .ps1
Cross-project pattern documented in vault:
00_meta/patterns/github-branch-hygiene.md
(added to the patterns _index.md under Git & CI/CD)
Tests: 17/17 green locally. dotfiles repo itself already has the setting
enabled (applied manually during the same session). Re-running the
script reports `Already enabled, nothing to do` and exits 0.
When to use
-----------
- New repo creation: run after `gh repo create`. Catches the setting
before the first PR.
- Existing repos: backfill with `--repo owner/name`. Idempotent on
already-enabled repos.
- Long-lived: the patches in the script are the only persistent state;
the script itself can be re-run any time without side effects.
Out of scope
------------
- Bulk audit of all the user's repos (script supports one --repo at a
time; a wrapper that lists all repos and applies the setting could be
added later, YAGNI for now)
- Other GitHub repo settings (squash/rebase/merge allowed flags,
default branch protection, etc.) -- this script does ONE thing well;
additional settings get their own scripts or future iterations
- GitLab / Codeberg / Bitbucket parity -- same concept, different APIs;
separate per-vendor scripts when needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
scripts/init-repo-github-defaults.{sh,ps1}-- small idempotent tool that enablesdelete_branch_on_merge: trueon any GitHub repo viagh api. Plus bats coverage and a cross-project vault pattern.Why
The 2026-05-18 marathon session (PRs #38-#45) left 8 orphan branches on origin because
gh pr mergewas used without--delete-branchon some calls and the repo did not havedelete_branch_on_mergeenabled. The retro-cleanup took 30 seconds but the policy fix prevents recurrence permanently.This is a cross-project pattern -- every GitHub repo benefits. The script is portable; the vault pattern (
00_meta/patterns/github-branch-hygiene.md) documents the when/why/how.What
scripts/init-repo-github-defaults.sh-- bash, idempotent. Derivesowner/namefrom origin remote (ssh + https forms),--dry-run,--help,--repooverride for cross-repo backfill.scripts/init-repo-github-defaults.ps1-- PowerShell parity. ASCII-only per project convention (PSScriptAnalyzer rulePSUseBOMForUnicodeEncodedFile).-DryRunswitch.tests/init-repo-github-defaults.bats-- 17 assertions: syntax, idempotent early-exit,--dry-run,--help, unknown-arg rejection, ASCII check on.ps1, parity between.shand.ps1._meta/patterns/github-branch-hygiene.md(created via Hive MCP) + entry in_meta/patterns/_index.mdunder "Git & CI/CD".Usage
Idempotent: if the setting is already enabled, the script prints
[OK] Already enabled, nothing to doand exits 0.Test plan
bats tests/init-repo-github-defaults.bats-- 17/17 green locallybash -n+zsh -non the.shclean--dry-runon this repo reports[OK] Already enabled(the dotfiles repo had the setting backfilled manually during the same session).ps1(PSScriptAnalyzer convention)lint,lint-powershell,test,integration)--repo owner/nameto validate cross-repo backfill worksOut of scope (deferred)
Cross-links
_meta/patterns/github-branch-hygiene.md_meta/patterns/_index.md(Git & CI/CD section)commit-commands:clean_gone(local-side counterpart -- deletes branches withgoneupstream)