如何将最后 N 次提交压缩为 git 中的单个提交?

How to squash the last N commits into a single commit in git?

我实际上想在一次提交中压缩 5-6 次提交。在这种情况下,我的最佳选择是什么?

签出最近的提交后,对您想要的父提交进行软重置。如果提交是线性的,最后 N 次提交的父级是 HEAD~N。例如,合并最后五个提交:

git reset --soft HEAD~5

(可选)确保您使用 git statusgit diff 进行了正确的更改。

然后创建新提交:

git commit -m 'five combined commits in one'