概述
Git 交互式 Rebase 工作流是一个初级难度的工作流。用交互式 rebase 在合并特性分支到 main 前压缩、重排或编辑提交。该工作流的目标是:Clean up commit history on a feature branch before merging to maintain a readable main branch.
环境准备
执行本工作流需要以下环境:A feature branch with multiple commits;Git CLI with interactive rebase support;Understanding of rebase commands。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。
执行步骤
本工作流包含4个步骤。步骤1:Start rebase——Run git rebase -i main to open the interactive rebase editor for the feature branch. 步骤2:Edit commits——Use squash, reword, or drop to clean up the commit history as desired. 步骤3:Complete rebase——Save and close the editor to apply the rebase, resolving conflicts if needed. 步骤4:Force push——Force push the rebased branch with git push –force-with-lease.
验证与清理
预期结果:The feature branch has a clean, readable commit history ready for merge into main.。验证方法:Run git log to confirm the commit history is clean; verify the branch builds and tests pass after rebase.。清理操作:Use git rebase –abort to cancel the rebase if something goes wrong.。estimatedMinutes 为 20 分钟。
代码示例
git rebase -i main
# In editor: pick, squash, reword as needed
git push --force-with-lease