使用gitk日志,我无法发现git merge和git merge -no-ff的效果之间的区别。我如何观察差异(使用git命令或一些工具)?


当前回答

其他答案很好地表明——no-ff会导致合并提交。这保留了关于特性分支的历史信息,这很有用,因为特性分支是定期清理和删除的。

这个答案可以提供何时使用或不使用的上下文——no-ff。

从特性合并到主分支:使用——no-ff

工作的例子:

$ git checkout -b NewFeature
[work...work...work]
$ git commit -am  "New feature complete!"
$ git checkout main
$ git merge --no-ff NewFeature
$ git push origin main
$ git branch -d NewFeature

将变更从main分支合并到feature分支:leave off—no-ff

工作的例子:

$ git checkout -b NewFeature
[work...work...work]
[New changes made for HotFix in the main branch! Lets get them...]
$ git commit -am  "New feature in progress"
$ git pull origin main
[shortcut for "git fetch origin main", "git merge origin main"]

其他回答

这是一个老问题,在其他文章中也有一些微妙的提及,但对我来说,这个问题的解释是,非快进合并将需要单独的提交。

——no-ff选项确保不会发生快速向前合并,并且总是会创建一个新的提交对象。如果您希望git维护特性分支的历史记录,这可能是可取的。              在上图中,左侧是使用git merge -no-ff后的git历史记录,右侧是使用git merge的示例,其中ff合并是可能的。

编辑:此图像的以前版本仅指示合并提交的单个父节点。合并提交有多个父提交,git用它们来维护“特性分支”和原始分支的历史记录。多个父链接用绿色突出显示。

——no-ff标志导致合并总是创建一个新的提交对象,即使合并可以用快进执行。这避免了丢失关于特性分支的历史存在信息,并将所有添加了该特性的提交组合在一起

合并策略

显式合并(又名非快进):创建一个新的合并提交。(这是你使用no-ff后得到的结果。)

快速向前合并:快速向前,不创建新的提交:

Rebase:建立一个新的基准面:

Squash:用力压碎或挤压(某物)使其变平:

其他答案很好地表明——no-ff会导致合并提交。这保留了关于特性分支的历史信息,这很有用,因为特性分支是定期清理和删除的。

这个答案可以提供何时使用或不使用的上下文——no-ff。

从特性合并到主分支:使用——no-ff

工作的例子:

$ git checkout -b NewFeature
[work...work...work]
$ git commit -am  "New feature complete!"
$ git checkout main
$ git merge --no-ff NewFeature
$ git push origin main
$ git branch -d NewFeature

将变更从main分支合并到feature分支:leave off—no-ff

工作的例子:

$ git checkout -b NewFeature
[work...work...work]
[New changes made for HotFix in the main branch! Lets get them...]
$ git commit -am  "New feature in progress"
$ git pull origin main
[shortcut for "git fetch origin main", "git merge origin main"]