在git中进行单独的合并提交的目的是什么? [英] What is the purpose of a separate merge commit in git?

查看:133
本文介绍了在git中进行单独的合并提交的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将两个分支合并在一起时,git会创建一个单独的提交,并带有类似Merge branch 'master' into my-branch的消息. 对于非平凡的合并,我很清楚这是有用的:它使事物合并的历史与任何其他更改分开.但是,当合并变得微不足道时(例如,两个分支不影响任何公共文件),单独的合并提交的目的是什么?

When you merge two branches together, git creates a separate commit with a message like Merge branch 'master' into my-branch. For a non-trivial merge it is clear to me that this is useful: It keeps the history of how things were merged separate from any other changes. But when the merge is trivial (for example when the two branches don't affect any files in common), what is the purpose of the separate merge commit?

您可以通过git merge --no-commit避免该提交.然后,您也可以在此提交上添加实际"更改.这样,您可以避免看似无用的额外提交.但是手册页对此进行了警告:

You can avoid that commit with git merge --no-commit. You could then add a "real" change on this commit too. That way you avoid a seemingly useless extra commit. But the man page warns about this:

您应该避免滥用此选项,以将大量更改潜入合并提交.可以接受一些小的修正,例如提高发行版/版本名称.

You should refrain from abusing this option to sneak substantial changes into a merge commit. Small fixups like bumping release/version name would be acceptable.

那是为什么?

其中具有 real 更改的提交标记为o,而仅合并的提交标记为X,这是这两种情况之间的区别:

With commits with real changes in them marked as o and merge-only commits as X, this is the difference between these two situations:

o---o-o-o---o--- master
 \   \   \   \
  o-o-X-o-X-o-X-o--- my-feature


o---o-o-o---o--- master
 \   \   \   \
  o-o-o---o---o--- my-feature

为什么第一个优先于第二个?

Why is the first one preferred over the second one?

PS:假设我的功能是其他人也使用的公共分支,因此 rebase 将不是一个选择.

PS: Assume my-feature is a public branch that other people also use, so rebase would not be an option.

推荐答案

我认为,单独进行合并提交是一个好主意,但有很多原因,但这都归结为基本规则,即每个提交都应包含更改在一起.

I think there are several reasons why having separate merge commits is a good idea, but it all comes down to the basic rule, that every commit should contain changes that belong together.

在可能的情况下,您将受益于单独的合并提交:

Possible situations where you will benefit from having separate merge commits:

  1. 您正在尝试找出哪个提交引入了错误.如果不将合并提交和普通提交分开,则很难确定合并是导致该错误还是对该提交进行了实际更改.
  2. 您要还原提交所做的更改.当您还原既是合并又包含更改的提交时,应该怎么办?您要仅还原更改,还是仅还原合并,还是两者都还原?如果只有其中一个,如何将它们分开?

作为额外的好处,您可以获得更易于理解的历史记录. (如果使用rebase,则可以获得更容易理解的历史记录,但是您说不可能.)

As added benefit, you get history that's easier to understand. (You could get history that's even easier to understand if you used rebase, but you said that's not possible.)

这篇关于在git中进行单独的合并提交的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆