git-branch-cleanup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Branch Cleanup

Git分支清理

Use this skill to safely remove merged or stale branches with explicit user confirmation.
使用此工具可通过用户明确确认来安全移除已合并或过时的分支。

Safety Rules

安全规则

  • Stop if the working tree is not clean.
  • Detect the default branch from
    origin/HEAD
    and fall back to
    main
    , then
    master
    .
  • Never delete protected branches.
  • Never force delete unless the user explicitly approves.
  • 如果工作区未处于干净状态则停止操作。
  • origin/HEAD
    检测默认分支,若失败则依次回退到
    main
    master
  • 绝不删除受保护分支。
  • 除非用户明确批准,否则绝不强制删除。

Workflow

工作流程

  1. Check repository state
    • git status --porcelain
    • If not clean, ask the user to commit or stash first.
  2. Detect default branch
    • git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
    • If that fails, try
      main
      , then
      master
      .
  3. Define protected branches
    • Default:
      main
      ,
      master
      ,
      develop
      ,
      staging
      ,
      production
      ,
      qa
      .
    • Ask the user if any additional branches should be protected.
  4. List merged branches
    • Local:
      git branch --merged <default-branch>
    • Remote:
      git branch -r --merged <default-branch>
    • Exclude protected branches and the current branch.
    • Present the list and ask for explicit approval to delete.
  5. List stale branches
    • Show last commit date for each local branch:
      git for-each-ref --format='%(refname:short) %(committerdate:short)' refs/heads
    • Ask user for a cutoff (default 30 days).
    • Present the stale list and ask for explicit approval to delete.
  6. Delete with confirmation
    • Local delete:
      git branch -d <branch>
    • If deletion fails because it is unmerged, ask whether to force delete with
      -D
      .
  7. Remote cleanup
    • Prune tracking branches:
      git remote prune origin
    • For remote branches approved for deletion:
      git push origin --delete <branch>
  8. Report and recovery
    • Summarize what was deleted.
    • Provide recovery hint:
      git reflog --no-merges --since="2 weeks ago"
      .
  1. 检查仓库状态
    • git status --porcelain
    • 如果工作区不干净,提示用户先提交或暂存更改。
  2. 检测默认分支
    • git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
    • 若上述命令失败,尝试
      main
      ,再尝试
      master
  3. 定义受保护分支
    • 默认值:
      main
      master
      develop
      staging
      production
      qa
    • 询问用户是否有其他需要保护的分支。
  4. 列出已合并分支
    • 本地分支:
      git branch --merged <default-branch>
    • 远程分支:
      git branch -r --merged <default-branch>
    • 排除受保护分支和当前分支。
    • 展示列表并请求用户明确批准删除。
  5. 列出过时分支
    • 显示每个本地分支的最后提交日期:
      git for-each-ref --format='%(refname:short) %(committerdate:short)' refs/heads
    • 询问用户设置截止日期(默认30天)。
    • 展示过时分支列表并请求用户明确批准删除。
  6. 确认后删除
    • 本地删除:
      git branch -d <branch>
    • 如果因分支未合并导致删除失败,询问用户是否使用
      -D
      强制删除。
  7. 远程仓库清理
    • 修剪跟踪分支:
      git remote prune origin
    • 对于已批准删除的远程分支:
      git push origin --delete <branch>
  8. 报告与恢复
    • 汇总已删除的分支信息。
    • 提供恢复提示:
      git reflog --no-merges --since="2 weeks ago"

Output Format

输出格式

Provide:
  • Default branch detected
  • Protected branch list
  • Merged branches list (local and remote)
  • Stale branches list (local)
  • Confirmations collected
  • Final summary of deletions
需提供:
  • 检测到的默认分支
  • 受保护分支列表
  • 已合并分支列表(本地和远程)
  • 过时分支列表(本地)
  • 收集到的确认信息
  • 删除操作的最终汇总