Git 合并还是 git 变基?

Git merge or git rebase?

我读过 git 普通合并适用于某些情况,而 git 变基适用于在 Git 中进行合并时的其他情况。

但我看不出变基如何替代合并。在对分支进行变基后,它仍然需要进行合并,这更可能是快速合并。因此,据我了解,Rebase 只是用于 "guarantee" 快速合并 - 它不能替代 git 合并。正确吗?

基本上就是这样(或者至少是使用变基的原因之一)。它保证了快进合并。 (我还发现当 rebase 与合并相反时更容易解决冲突,尤其是在使用 git rerere 时。)

请注意,您最终得到的最终提交指向的快照,无论它是最后一个变基提交变基还是最终合并提交合并后,是相同的快照只是历史有所不同。 重放重放更改按照它们被引入的顺序从一行工作到另一行,而合并采用端点并将它们合并在一起。

以上是一个很好的总结!

我会说这两个东西在操作上是根本不同的,但实现的东西是相似的。

git rebase 将您的提交列表放在另一个分支之上的一个分支中。因此,冲突解决是按照您定义的顺序(如果使用 --interactive)在每次提交中完成的,如果您没有将它们完全正交,则每个提交都会产生冲突(即:提交可能不适用,因为它们中的每一个都是单独应用)。 请注意您还可以在变基时将多个提交合并为一个提交 - 所谓的压缩 -

git merge有不同的策略,它结合了多个分支。 另请参阅合并帮助,了解您可以选择自动冲突解决策略(我将挑选一些并粘贴部分摘要):

  • ours:此选项通过支持我们的版本强制自动解决冲突的帅哥。
  • octopus:这解决了超过两个头的情况,但拒绝进行需要手动解决的复杂合并。
  • subtree:这是一个修改过的递归策略。合并树A和B时,如果B对应A的子树,则先调整B匹配A的树结构,而不是读取同级树

也就是说,rebase 只是当没有人看到您的更改时的一个选项,因为它会更改提交哈希并且合并始终适用。另请注意,如果您有审查流程,变基可以让您选择更改更改历史记录以在日志中反映您的意图。

我想 Git Bucket 的这篇文章会给你答案。

Merging vs. Rebasing

The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way. This avoids all of the potential pitfalls of rebasing (discussed below).

On the other hand, this also means that the feature branch will have an extraneous merge commit every time you need to incorporate upstream changes. If master is very active, this can pollute your feature branch’s history quite a bit. While it’s possible to mitigate this issue with advanced git log options, it can make it hard for other developers to understand the history of the project.

The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk.

But, there are two trade-offs for this pristine commit history: safety and traceability. If you don’t follow the Golden Rule of Rebasing, re-writing project history can be potentially catastrophic for your collaboration workflow. And, less importantly, rebasing loses the context provided by a merge commit—you can’t see when upstream changes were incorporated into the feature.