概述
Git 分支清理工作流是一个初级难度的工作流。识别并删除已合并到主分支的本地分支,保持仓库整洁避免混淆。该工作流的目标是:Remove stale local branches after merge to prevent confusion and reduce clutter in the developer environment.
环境准备
执行本工作流需要以下环境:A local Git repository with multiple branches;Git CLI version 2.0 or later;Shell terminal access。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。
执行步骤
本工作流包含3个步骤。步骤1:List all local branches——Run git branch to see all local branches and identify which ones exist. 步骤2:Check merged branches——Run git branch –merged main to identify branches that have been fully merged. 步骤3:Delete merged branches——Pipe the merged branch list to git branch -d for each branch name excluding main.
验证与清理
预期结果:All local branches that have been merged into main are deleted, and only active or unmerged branches remain.。验证方法:Run git branch again and confirm that no merged branches remain; verify that main and active feature branches are still present.。清理操作:No cleanup needed beyond the branch deletion itself.。estimatedMinutes 为 15 分钟。
代码示例
git branch --merged main | grep -v "main" | xargs git branch -d