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