交互式 rebase (git rebase -i) 允许你重写、重新排序、合并、编辑或删除提交 — 在分享之前清理你的提交历史。它是从混乱的进行中工作提交制作干净、逻辑清晰历史的强大工具。
启动交互式 rebase
bash
git rebase -i HEAD~4 # interactively rebase the last 4 commits
git rebase -i <commit> # rebase commits after <commit>
# → opens an editor listing the commits with actions to apply
操作
text
pick abc123 Add login form # keep the commit as-is
squash def456 Fix typo # COMBINE into the previous commit (merge messages)
fixup ghi789 Fix another typo # combine into previous, DISCARD this message
reword jkl012 Add validation # keep the commit but EDIT its message
edit mno345 Refactor auth # PAUSE to amend the commit (change content)
drop pqr678 Debug logging # DELETE this commit entirely
# reorder lines to REORDER commits
