如何针对同一个 git 修订版应用两个补丁?

发布于 2024-10-21 02:16:18 字数 539 浏览 2 评论 0原文

这是一个想象的问题,但我在补丁方面遇到了真正的问题。假设我有一个具有以下 git 历史记录的项目:

A - B - C

现在,如果我收到两个补丁,C1C2,它们应该应用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘曦 2024-10-28 02:16:18

经典的方法是:

  • 应用收到的第一个补丁,
  • 拒绝任何不快进的补丁,并要求发送者首先重新设置他/她的存储库,然后重新检查补丁并重新发送。

一般的想法是,解决合并冲突不取决于您:只有补丁的创建者才拥有解决与当前源代码的任何冲突所需的知识。

正如 Linus Torvalds(Git 的创建者)在 他 2007 年 Google 演讲中所说:

所以,记住,分布意味着没有人是特别的。
因此,我不是合并,而是推出我的第一棵树,它没有任何合并问题,然后我告诉第二个人:

“嘿,我试图从你那里拉出来,但我遇到了合并冲突,而且它们并不是完全微不足道的,所以我决定你来完成这项荣誉。”

他们确实这么做了。他们知道自己在做什么,因为这是他们的改变。所以他们可以进行合并,他们可能认为我是个白痴,因为合并是如此简单,很明显我应该采用他们的代码,但他们进行合并并更新他们的树,并说“嘿,你能拉现在就从我这里开始”,我从他们那里拉出来,他们为我做了所有的工作。

这就是全部:他们为我做了所有的工作。所以,...我承担了功劳。现在我只需要弄清楚第三步:利润。


The classic way is to:

  • apply the received first patch,
  • refuse any patch which isn't fast-forward and ask the sender to rebase his/her repo first, then re-check the patch, and resend it.

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:

So what happens is, remember, distribution means nobody is special.
So instead of me merging, I just push out my first tree, that did not have any merge issues, and I tell the second person:

"hey, I tried to pull from you, but I had merge conflicts and they weren't completely trivial, so I decided you get to do the honors instead."

And they do. And they know what they are doing because it's their changes. So they can do the merges and they probably think I am a moron because the merge was so easy and it is obvious I should have taken their code, but they do the merge and they update their tree, and say "hey, can you pull from me now", and I pull from them and they did all the work for me.

That's what is all about: they did all the work for me. So,... and I take the credit. Now I just need to figure out the step 3: profit.

寂寞美少年 2024-10-28 02:16:18

大多数时候,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.

柒夜笙歌凉 2024-10-28 02:16:18

您至少完全可以尝试应用所有这些补丁。您可能会遇到合并冲突,正如 VonC 所建议的那样,您可能希望补丁提交者解决这些问题,或者您也可以像 cjb 所说的那样自己解决!无论如何,这就是您想要做的。

第一种情况:两个补丁,C1 和 C2。

# apply C1
git am C1.patch
# create a temporary branch (use a real name instead of C2)
git checkout -b C2 C
# apply C2
git am C2.patch
# return to master
git checkout master
# merge the other branch
git merge C2
# and delete the other branch
git branch -d 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.

# apply C1
git am C1.patch
# create a temporary branch (use a real name instead of C2)
git checkout -b C2 C
# apply C2
git am C2.patch
# return to master
git checkout master
# merge the other branch
git merge C2
# and delete the other branch
git branch -d 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文