Git:如何为合并创建补丁? [英] Git: How to create patches for a merge?

查看:505
本文介绍了Git:如何为合并创建补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 git format-patch 时,它似乎没有包含合并。如何执行合并,然后通过电子邮件将它发送给某人作为一组修补程序?



例如,假设我合并了两个分支并在另一个分支上执行另一个提交合并顶部:

  git init 

echoinitial file> test.txt
git add test.txt
git commit -mCommit A

git checkout -b foo master
echofoo> test.txt
git commit -a -mCommit B

git checkout -b bar master
echobar> test.txt
git commit -a -m提交C

git合并foo
echofoobar> test.txt
git commit -a -mCommit M

echo2nd line>> test.txt
git commit -a -m提交D

这会创建以下tree:

  B 
/ \
AM - D
\ /
C

现在我尝试检出初始提交并重播上述更改:

  git checkout -b replay master 
git format-patch --stdout master..bar | git am -3

这会产生合并冲突。在这种情况下, git format-patch master..bar 只产生3个补丁,省略Commit M。如何处理这个问题?



-Geoffrey Lee

解决方案

如果您检查前两个补丁的内容,您会看到问题:

  diff --git a / test.txt b / test.txt 
--- a / test.txt
+++ b / test.txt
@@ -1 +1 @@
- 初始文件
+ foo

diff --git a / test.txt b / test.txt
index 7c21ad4..5716ca5 100644
--- a / test.txt
+++ b / test.txt
@@ -1 +1 @@
- 初始文件
+ bar

从你当时正在工作的分支(foo和bar)的角度来看,这两个提交都删除了初始文件行,并用其他内容代替完全。 AFAIK,当你生成一个具有重叠变化的非线性进程的补丁(你的分支在这种情况下提交B和C)时,无法避免这种冲突。



<人们通常使用补丁来添加单个功能或修复已知良好的先前工作状态的错误 - 修补程序协议根本不够复杂,无法处理合并历史记录,就像Git本身所做的那样。如果你想让别人看到你的合并,那么你需要在分支之间推/拉,而不是放弃差异/补丁。


When I use git format-patch, it doesn't seem to include merges. How can I perform a merge and then e-mail it to someone as a set of patches?

For example, let's say that I merge two branches and perform another commit on top of the merge:

git init

echo "initial file" > test.txt
git add test.txt
git commit -m "Commit A"

git checkout -b foo master
echo "foo" > test.txt
git commit -a -m "Commit B"

git checkout -b bar master
echo "bar" > test.txt
git commit -a -m "Commit C"

git merge foo
echo "foobar" > test.txt
git commit -a -m "Commit M"

echo "2nd line" >> test.txt
git commit -a -m "Commit D"

This creates the following tree:

    B
  /   \
A       M - D 
  \   /
    C

Now I try to checkout the initial commit and replay the above changes:

git checkout -b replay master
git format-patch --stdout master..bar | git am -3

This produces a merge conflict. In this scenario, git format-patch master..bar only produces 3 patches, omitting "Commit M". How do I deal with this?

-Geoffrey Lee

解决方案

If you examine the content of the first two patches you'll see the issue:

diff --git a/test.txt b/test.txt
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-initial file
+foo

diff --git a/test.txt b/test.txt
index 7c21ad4..5716ca5 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-initial file
+bar

from the perspective of the branch you were working on at the time (foo and bar) both of these commits have removed the "initial file" line and replaced it with something else entirely. AFAIK, there's no way to avoid this kind of conflict when you generate a patch of a non-linear progression with overlapping changes (your branch commits B and C in this case).

People normally use patches to add a single feature or bug fix off a known good prior work state -- the patch protocol is simply not sophisticated enough to handle merge history like Git does natively. If you want someone to see your merge then you need to push/pull between branches not drop back diff/patch.

这篇关于Git:如何为合并创建补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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