branch-cleanup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

branch-cleanup

branch-cleanup

Report stale remote branches and propose safe deletion. Never delete a branch without explicit authorization for that batch.
报告陈旧的远程分支并提出安全删除建议。未经当前批次的明确授权,绝不删除任何分支。

What this skill is NOT for

本 skill 不适用于

  • Local git worktree management (
    git worktree add
    ,
    list
    ,
    prune
    ) — out of scope.
  • One-time repository merge-default and branch-protection setup — use
    /gh-bootstrap
    . This skill only reads
    delete_branch_on_merge
    and existing rulesets; it proposes turning the setting on but the write happens through
    gh api -X PATCH
    , the same endpoint
    /gh-bootstrap
    uses in its step 2.
  • 本地 Git worktree 管理(
    git worktree add
    、
    list
    、
    prune
    )——不在范围内。
  • 一次性设置 repository 合并默认值和 branch-protection rulesets——请使用
    /gh-bootstrap
    。 本 skill 只读取
    delete_branch_on_merge
    和现有 rulesets;它会提议开启此设置, 但实际写入通过
    gh api -X PATCH
    完成,与
    /gh-bootstrap
    在步骤 2 中使用的 endpoint 相同。本 skill 不重复该步骤。

Protocol

协议

1. Check delete-branch-on-merge

1. 检查 delete-branch-on-merge

bash
REPO="$(gh repo view --json nameWithOwner --jq '.nameWithOwner')"
gh api "repos/$REPO" --jq '.delete_branch_on_merge'
If
false
, propose enabling it and explain the effect: GitHub deletes a branch's remote ref automatically once its pull request merges. Ask for explicit authorization before running the
gh api -X PATCH repos/{owner}/{repo} -F delete_branch_on_merge=true
write — see
/gh-bootstrap
step 2 for the full merge-defaults
PATCH
, which this skill does not duplicate.
bash
REPO="$(gh repo view --json nameWithOwner --jq '.nameWithOwner')"
gh api "repos/$REPO" --jq '.delete_branch_on_merge'
如果结果为
false
,则提议启用此设置并说明其效果:pull request 合并后,GitHub 会自动删除该分支的 remote ref。在运行
gh api -X PATCH repos/{owner}/{repo} -F delete_branch_on_merge=true
写入操作前,必须获得明确授权——完整的 merge-defaults
PATCH
请参阅
/gh-bootstrap
步骤 2,本 skill 不予重复。

2. Report stale branches

2. 报告陈旧分支

Run the bundled script from the repository root:
bash
python3 "$BRANCH_CLEANUP_HELPER" --days 90 --github "$REPO"
Resolve
BRANCH_CLEANUP_HELPER
from the loaded
SKILL.md
directory (
scripts/stale_branches.py
), resolving symlinks first. For each remote branch the script reports:
  • Last commit date and author (
    git log -1
    ).
  • Ahead/behind counts against the default branch (
    git rev-list --left-right --count
    ).
  • Merged status (
    git branch -r --merged <default>
    ).
  • Whether an open pull request targets the branch (
    gh pr list --head <branch> --state open --json number
    ), when
    --github
    is passed.
  • Whether a ruleset protects the branch (
    gh api repos/{owner}/{repo}/rules/branches/ {branch}
    ), when
    --github
    is passed.
The output is a table sorted by age (oldest first) with a
recommendation
column of
keep
,
archive
, or
delete
. See
references/policy.md
for the exact classification rule.
从 repository 根目录运行随附的脚本:
bash
python3 "$BRANCH_CLEANUP_HELPER" --days 90 --github "$REPO"
从已加载的
SKILL.md
所在目录解析
BRANCH_CLEANUP_HELPER
(
scripts/stale_branches.py
),并首先解析 symlink。对于脚本报告的每个远程分支:
  • 最后一次提交的日期和作者(
    git log -1
    )。
  • 相对默认分支的 ahead/behind 数量(
    git rev-list --left-right --count
    )。
  • 合并状态(
    git branch -r --merged <default>
    )。
  • 是否有未关闭的 pull request 以该分支为目标(
    gh pr list --head <branch> --state open --json number
    ),前提是传入了
    --github
    。
  • 该分支是否受到 ruleset 保护(
    gh api repos/{owner}/{repo}/rules/branches/{branch}
    ),前提是传入了
    --github
    。
输出结果是一张按分支年龄排序( oldest first)的表格,其中
recommendation
列的值为
keep
、
archive
或
delete
。准确的分类规则请参阅
references/policy.md
。

3. Local hygiene

3. 本地清理

Report-only, no deletion:
bash
git fetch --prune
git branch --merged <default>
List the locally merged branches for the user. Do not delete local branches without the same per-batch authorization required in step 4.
仅报告,不删除:
bash
git fetch --prune
git branch --merged <default>
向用户列出本地已合并的分支。除非获得步骤 4 所要求的同等级逐批次授权,否则不得删除本地分支。

4. Deletion is never automatic

4. 绝不自动删除

For every branch classified
delete
, print the exact command:
bash
git push origin --delete <branch>
Always exclude the default branch, any protected branch, and any branch with an open pull request from deletion — the script enforces this by classifying those branches
keep
regardless of age. Require explicit user authorization before running any printed command, and confirm the authorized batch matches the printed list exactly.
对于每个被分类为
delete
的分支,输出精确命令:
bash
git push origin --delete <branch>
始终将默认分支、任何受保护分支以及任何存在未关闭 pull request 的分支排除在删除范围之外——无论分支年龄如何,脚本都会将这些分支分类为
keep
。运行任何已输出的命令前,都必须获得用户的明确授权,并确认授权批次与输出列表完全一致。

5. Optional scheduled report workflow

5. 可选的定时报告工作流

Copy
assets/stale-branch-report.yml
to
.github/workflows/stale-branch-report.yml
if the user wants a recurring report. It only writes a job summary — it never deletes a branch.
actions/checkout
is pinned by SHA.
如果用户需要定期报告,请将
assets/stale-branch-report.yml
复制到
.github/workflows/stale-branch-report.yml
。该工作流只写入 job summary,绝不会删除分支。
actions/checkout
已通过 SHA 固定版本。

6. Verify

6. 验证

Re-run the script and confirm the printed table matches the branches actually present on the remote (
git branch -r
). Confirm no
delete
recommendation includes the default branch, a protected branch, or a branch with an open pull request.
重新运行脚本,并确认输出的表格与远程实际存在的分支(
git branch -r
)一致。确认任何
delete
建议均不包含默认分支、受保护分支或存在未关闭 pull request 的分支。

Cross-references

交叉引用

  • /gh-bootstrap
    — one-time repository merge defaults and branch-protection rulesets.
  • /prek
    — local pre-commit hook checks, unrelated to branch lifecycle.
  • /oss-hygiene
    — broader repository health, including community-standards files.
  • /gh-bootstrap
    ——一次性设置 repository 合并默认值和 branch-protection rulesets。
  • /prek
    ——本地 pre-commit hook 检查,与分支生命周期无关。
  • /oss-hygiene
    ——更全面的 repository 健康检查,包括 community-standards 文件。