| name | source-command-ccg-clean-branches |
| description | 清理 Git 分支:安全清理已合并或过期分支,默认 dry-run 模式 |
source-command-ccg-clean-branches
Use this skill when the user asks to run the migrated source command ccg-clean-branches.
Command Template
Clean-Branches - 清理 Git 分支
安全识别并清理已合并或长期未更新的分支。
使用方法
/clean-branches [options]
选项
| 选项 | 说明 |
|---|
--base <branch> | 基准分支(默认 main/master) |
--stale <days> | 清理超过 N 天未更新的分支 |
--remote | 同时清理远程分支 |
--dry-run | 只预览,不执行(默认) |
--yes | 跳过确认直接删除 |
--force | 强制删除未合并分支 |
执行工作流
🔍 阶段 1:预检
[模式:准备]
- 同步远端:
git fetch --all --prune
- 读取保护分支配置
- 确定基准分支
📋 阶段 2:分析识别
[模式:分析]
已合并分支:
过期分支(如指定 --stale):
排除:
📊 阶段 3:报告预览
[模式:报告]
## 将要删除的分支
### 已合并分支
- feature/old-feature (合并于 3 天前)
- bugfix/fixed-issue (合并于 7 天前)
### 过期分支
- experiment/old-test (最后更新 90 天前)
✅ 阶段 4:执行清理
[模式:执行]
仅在不带 --dry-run 且确认后执行:
git branch -d <branch>
git push origin --delete <branch>
git branch -D <branch>
保护分支配置
git config --add branch.cleanup.protected develop
git config --add branch.cleanup.protected 'release/*'
git config --get-all branch.cleanup.protected
示例
/clean-branches --dry-run
/clean-branches --stale 90
/clean-branches --base release/v2.1 --remote --yes
最佳实践
- 优先 dry-run – 先预览再执行
- 活用 --base – 适配 release 工作流
- 谨慎 --force – 除非确定无用
- 团队协作 – 清理远程分支前先通知
- 定期运行 – 每月/季度一次保持清爽