将最新提交压缩为最后一次合并提交

Squashing latest commits into last merge commit

这是我的结构:

* - commit 2
|
* - commit 1
|
* - Merge commit (feature #1)
|\  
| * - Feature #1 commit 
|/
*

我之前搞砸了,现在我的历史看起来不像我想要的那么干净。

我想知道是否有一种简单的方法来压缩 'commit 1''commit 2'进入'Merge commit (feature #1)',所以我最终得到这样的东西:

* - Merge commit (feature #1)
|\  
| * - Feature #1 commit 
|/
*

rebase -i 似乎无法识别合并提交,并且似乎会将所有内容压缩为 'Feature #1 commit',这是不可取的。
谢谢

确保您没有任何预演 (git added)。然后做

git reset --soft HEAD~2       # go back across the two commits
git commit --amend            # squash into the merge commit

git reset --soft 不会更改工作目录中的内容,也不会更改暂存的内容。因此,如果您在做出“提交 2”后 git add 编辑了内容,那么 git commit --amend 也会提交那些暂存的内容。出于这个原因,您必须确保在开始时没有上演任何内容。 git status 应该告诉你是否是这种情况。