Git 合并 patch 时的冲突处理一例

来源:互联网 发布:js 取反运算符 编辑:程序博客网 时间:2024/06/03 17:24

from : http://www.fwolf.com/blog/post/448

git version 1.6.0.4

合并 patch 的时候,如果生成 patch 的“原稿”找不到,一般就产生了冲突,比如:

由于这些patch显然是用git format-patch来生成的,所以用git的工具应该就可以很好的做好。git-am 就是作这件事情。

$ git am 0001-BUG-Sybase.patchApplying: CHG: 读取Sybase如果时间为空,设置默认时间的修改error: patch failed: source.php:38error: source.php: patch does not applyPatch failed at 0001.When you have resolved this problem run "git am --resolved".If you would prefer to skip this patch, instead run "git am --skip".To restore the original branch and stop patching run "git am --abort".

在使用git-am之前, 你要首先git am –abort 一次,来放弃掉以前的am信息,这样才可以进行一次全新的am。
不然会遇到这样的错误。

刚开始一看有些懵,因为没有任何冲突在哪里的提示,后来找到一种方法,am 操作出问题后先手工 apply:

$ git apply --reject 0001-BUG-Sybase.patchChecking patch source.php...error: while searching for:        // 注释        // 以下为几行代码片断error: patch failed: source.php:38Applying patch source.php with 1 rejects...Rejected hunk #1.

这样,就把没有冲突的文件先合并了,剩下有冲突的作了标记。先看输出,error: while searching for: 说明是这段代码有冲突,error: patch failed: source.php:38 指明了产生冲突的代码片断的开始行号,相应的,patch 中应该有这么一段:

diff --git a/source.php b/source.phpindex 8770441..4e77b8a 100644--- a/source.php+++ b/source.php@@ -38,27 +38,23 @@ class Site extends Module        // 注释        // 以下为几行代码片断

同时,还会产生一个 source.php.rej 文件,里面也是上面这段因为冲突无法合并的代码片断。

现在,在这段代码中查找冲突原因,并对文件进行修改,source.php.rej 参考完了可以删掉。改好之后,用 git add 把 source.php 添加到缓冲区,同时也要把其他没有冲突合并成功了的文件也加进来,因为在作 apply 操作的时候他们也发生了变化:

$ git add source.php$ git add 其他 apply 进来的文件们

最后:

$ git am --resolvedApplying: CHG: 读取Sybase如果时间为空,设置默认时间的修改

git commit --amend --author="Author Name <email@address.com>" //修改原作者



0 0
原创粉丝点击