You can do this fairly easily without git rebase
or git merge --squash
.
(您可以很容易地做到这一点,而无需git rebase
或git merge --squash
。)
In this example, we'll squash the last 3 commits. (在此示例中,我们将压缩最后3次提交。)
If you want to write the new commit message from scratch, this suffices:
(如果您想从头开始编写新的提交消息,则满足以下条件:)
git reset --soft HEAD~3 &&
git commit
If you want to start editing the new commit message with a concatenation of the existing commit messages (ie similar to what a pick/squash/squash/…/squash git rebase -i
instruction list would start you with), then you need to extract those messages and pass them to git commit
:
(如果要开始使用现有提交消息的串联来编辑新的提交消息(即类似于pick / squash / squash /…/ squash git rebase -i
指令列表的开头),则需要提取这些消息并将它们传递给git commit
:)
git reset --soft HEAD~3 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
Both of those methods squash the last three commits into a single new commit in the same way.
(这两种方法都以相同的方式将最后三个提交压缩为单个新提交。)
The soft reset just re-points HEAD to the last commit that you do not want to squash. (软重置只是将HEAD重新指向您不想挤压的最后一次提交。)
Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit (ie it already has all the changes from the commits that you are about to “throw away”). (软复位既不会触及索引也不会影响工作树,从而使索引处于您的新提交所需的状态(即,它已经具有您要“丢弃”的提交中的所有更改)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…