git rebase 是否比 git merge 产生更多的冲突?

Does git rebase create more conflicts than git merge?

git rebasegit merge 更容易发生冲突是真的吗?我以前听说过 this post

I am speculating based on anecdata here, but I suspect the general anxiety around rebasing stems from two primary places:

  1. Due to the mechanics of git rebase, merge conflicts are more frequent and seemingly harder to deal with

作者没有详细说明这种说法,但这不是我第一次听到。

我听说过并理解这样的观点,因为 rebase 会逐个提交地重放,你最终会多次遇到同样的冲突,但我从未遇到过这种情况。也许 rerere 行为现在是 git rebase 中的默认行为?

我正在尝试在我的团队中提出变基策略,但如果它是准确的,我想解决这个问题。

老实说,我预计会有相同数量的冲突,因为无论是变基还是合并,两个分支中的更改最终都会发生冲突。也就是说,冲突不是最后分支收敛引起的,而是同一行并行变化引起的

如果你有多个提交,你最终会与变基发生更多冲突。这是因为当您变基时,您必须解决每次提交的冲突。这意味着,如果您尝试变基一个比您的 master 提前 5 次提交的分支,并且您在这 5 次提交中的第一次提交中引入了合并冲突,您将必须在以下每次提交中解决该冲突。

经常重新设置基准应该可以解决上述问题。另外值得一提的是,如果你想压缩到单个提交,有一个比 rebase 更简单的策略。

假设您有 5 个提交要合并到 master 中,但这只是一个可以在一个提交中使用描述性消息轻松描述的功能。

git fetch
git merge origin/master
git reset --soft origin/master
git commit

如果你只想要一次提交,这会更容易,并且可以完成与变基相同的事情。