branch-cleaner

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Branch cleaner

分支清理工具

Goal

目标

Safely identify stale branches and provide explicit delete/prune commands.
安全识别过时分支,并提供明确的删除/修剪命令。

Inputs to confirm (ask if missing)

需要确认的输入信息(缺失时询问用户)

  • Default branch (main/master/develop).
  • Remote name (origin) and whether remote deletion is desired.
  • Safety rules: keep patterns (release/, hotfix/), minimum age, merged-only.
  • 默认分支(main/master/develop)。
  • 远程仓库名称(通常为origin)以及是否需要删除远程分支。
  • 安全规则:需保留的分支模式(如release/、hotfix/)、分支最小存在时长、仅清理已合并分支。

Workflow

工作流程

  1. Sync and inspect
    • Run
      git fetch --prune
      .
    • Check
      git status
      and note uncommitted changes.
  2. Build candidate lists
    • Local merged into default:
      git branch --merged <base>
    • Local not merged (list only):
      git branch --no-merged <base>
    • Remote merged:
      git branch -r --merged <base>
    • Stale by date:
      git for-each-ref --sort=committerdate refs/heads --format="%(committerdate:short) %(refname:short)"
  3. Exclude protected branches
    • Always keep
      <base>
      , current branch, and user-provided patterns.
  4. Confirm with user
    • Present candidates grouped by local vs remote.
  5. Provide delete commands
    • Delete branches approved for deletion by the user
  1. 同步并检查
    • 执行
      git fetch --prune
      命令。
    • 运行
      git status
      并记录未提交的更改。
  2. 生成候选分支列表
    • 已合并到默认分支的本地分支:
      git branch --merged <base>
    • 未合并到默认分支的本地分支(仅列出):
      git branch --no-merged <base>
    • 已合并的远程分支:
      git branch -r --merged <base>
    • 按日期筛选的过时分支:
      git for-each-ref --sort=committerdate refs/heads --format="%(committerdate:short) %(refname:short)"
  3. 排除受保护分支
    • 始终保留默认分支
      <base>
      、当前分支以及用户指定需保留的分支模式。
  4. 与用户确认
    • 将候选分支按本地和远程分组展示。
  5. 提供删除命令
    • 为用户确认删除的分支提供对应的删除命令

Optional GitHub CLI checks

可选GitHub CLI检查

  • gh pr list --state merged --base <base>
    to correlate merged branches.
  • gh pr view <branch>
    to verify status if needed.
  • 使用
    gh pr list --state merged --base <base>
    关联已合并的分支。
  • 必要时使用
    gh pr view <branch>
    验证分支状态。

Deliverables

交付内容

  • Candidate lists and rationale.
  • Warnings for unmerged or recently updated branches.
  • Don't remove remote branches unless explicitly approved.
  • 候选分支列表及筛选依据。
  • 针对未合并或最近更新分支的警告信息。
  • 除非用户明确批准,否则不得删除远程分支。