如何针对同一个 git 修订版应用两个补丁?
这是一个想象的问题,但我在补丁方面遇到了真正的问题。假设我有一个具有以下 git 历史记录的项目:
A - B - C
现在,如果我收到两个补丁,C1
和 C2
,它们应该应用于 C,我应该如何处理它们?如果我先应用补丁
C1
,那么我将无法应用补丁 C2
,因为存储库已变为:
A - B - C - C1
是否可以同时应用它们,或者执行以下操作:我必须回复发送 C2 的人,告诉他/她更新补丁?
现在假设我离线工作并提交,以便存储库变为:
A - B - C - D - E
然后我检查电子邮件并收到 的补丁C。 同样,是否可以简单地应用该补丁,或者我是否必须要求更新该补丁?
This is a imaginary problem, but I'm having real problems with patches. Let's say I have a project with the following git history:
A - B - C
Now if I receive two patches, C1
and C2
, that are meant to be applied on C
, how should I handle them? If I apply patch C1
first, then I will not be able to apply patch C2
because the repository has become:
A - B - C - C1
Is it possible to apply them both, or do I have to reply to the person sending C2 telling him/her to update the patch?
Now suppose I go offline and work and commit so that the repository becomes:
A - B - C - D - E
Then I check my email and receive a patch for C
. Again, is it possible to simply apply that patch, or do I have to ask for an update to the patch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经典的方法是:
一般的想法是,解决合并冲突不取决于您:只有补丁的创建者才拥有解决与当前源代码的任何冲突所需的知识。
正如 Linus Torvalds(Git 的创建者)在 他 2007 年 Google 演讲中所说:
The classic way is to:
The general idea is that it isn't up to you to solve merge conflicts: only the creator of the patch has the necessary knowledge to solve any conflict with the current source code.
As Linus Torvalds (creator of Git) said in his 2007 Google talk:
大多数时候,C2 将在 C1 之上应用。仅当它们对同一文件的重叠部分进行编辑时,您才会遇到合并冲突。 Git 将获取补丁中不冲突的所有部分,并插入冲突标记来帮助您解决合并问题。
至于做什么,这取决于你的项目有多大以及你在这部分代码中的能力如何——我总是自己解决冲突,只要提交者使用最近的结帐作为基础为他们的补丁。
另一位评论者提到莱纳斯的名言,说他总是让其他人解决冲突,但即使这也不完全正确;他经常要求人们在发送拉取请求时留下未解决的冲突,以便他能够解决这些问题。
Most of the time, C2 will apply on top of C1. It's only if they're edits to overlapping sections of the same files that you'll get a merge conflict. Git will take all the parts of the patch that don't conflict, and insert conflict markers that help you resolve the merge.
As for what to do, it depends how large your project is and how competent you are in that section of the code -- I've always just fixed up the conflicts myself, as long as the submitter used a reasonably recent checkout as the base for their patch.
Another commenter mentioned Linus' quote saying that he always makes other people resolve conflicts, but even this isn't quite true; he often asks people to leave conflicts unresolved when sending pull requests so he gets a shot at them.
您至少完全可以尝试应用所有这些补丁。您可能会遇到合并冲突,正如 VonC 所建议的那样,您可能希望补丁提交者解决这些问题,或者您也可以像 cjb 所说的那样自己解决!无论如何,这就是您想要做的。
第一种情况:两个补丁,C1 和 C2。
第二种情况可以类似处理;只是您自己没有应用 C1,而是提交了 D 和 E。
当然,如果合并失败,而且看起来太可怕了,您无法自行整理,只需将其清除并告诉 C2 的提交者自行修复即可。 (
git重置--merge; git分支-D C2
)You can totally at least try to apply all these patches. You may potentially get merge conflicts, and as VonC suggests, you might want the patch submitters to solve them, or you might do it yourself as cjb says! In any case, here's what you want to do.
First case: two patches, C1 and C2.
You can handle the second case similarly; it's just that instead of applying C1 you've made commits D and E yourself.
And of course, if the merge fails, and it looks too scary for you to sort through yourself, just blow it away and tell the submitter of C2 to fix it on their end. (
git reset --merge; git branch -D C2
)