使用交互式变基压缩时,Git跳过合并提交

4
我正在使用git开发一个功能,目前已完成以下步骤。
  • 基于master检出新的功能分支feature1
  • 添加了几个提交。
  • 基于feature1检出新分支test
  • test中进行一些更改。
  • 返回(checkout to)feature1分支。
  • test分支合并到feature1分支
  • 删除test分支
  • 编辑文件,并修改最后一个提交以反映更改

这是git log --oneline -n 5的输出

de5d701 Merge branch 'test' into 'feature1' (amended)
a668f93 'feature1' commit3
ded7c00 'feature1' commit2
8731807 Initial 'feature1' commit
ff2f539 latest 'master' commit

我希望在合并到主分支之前压缩ff2f539 latest 'master' commit后的所有内容。我尝试执行此操作,但遇到了问题。
git rebase --interactive  ff2f539

但是最新的提交在编辑器中没有列出:

pick 8731807 Initial 'feature1' commit
pick ded7c00 'feature1' commit2
pick a668f93 'feature1' commit3

# Rebase ff2f539..de5d701 onto ff2f539
#
# Commands:
...
# Note that empty commits are commented out

我不明白为什么将test分支合并到feature1分支后的最新提交被跳过了。 编辑器中的内容如下:
pick 8731807 Initial 'feature1' commit
squash ded7c00 'feature1' commit2
squash a668f93 'feature1' commit3

# Rebase ff2f539..de5d701 onto ff2f539
#
# Commands:
...
# Note that empty commits are commented out

产生一个新的提交,该提交不包含上一个提交所做的更改。
de5d701 Merge branch 'test' into feature1(amended)

他们迷失了。在合并到主分支之前,有什么帮助将所有这些提交压缩成一个单独的提交的方法吗?

你确定 ded7c00 是来自 feature1 分支而不是测试分支吗? - TheGeorgeous
2个回答

3
Git 设计上不会在“交互式”列表中显示合并提交。相反,它包含了通过该合并引入分支的所有提交。您可以使用-p (--preserve-merges)选项来更改此行为,但在交互模式下不建议这样做,因为结果可能不那么明显(请参见http://git-scm.com/docs/git-rebase#_bugs)。
但是,有一个简单的解决方案,而无需进行变基:
# make sure your working copy is clean, do git stash if needed
git checkout feature1
git reset --soft ff2f539
git commit -m "..."

这将生成一个提交,包括 ff2f539 之后的所有更改。

1
如果您想在变基中包含合并提交,请将--preserve-merges选项传递给git rebase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接