Git format-patch和Git am

    xiaoxiao2022-07-07  207

    git format-patch -s 4e16          // 某次提交以后的所有patch, --4e16指的是SHA1 ID, -s : signed off

    git format-patch -1                  //  单次提交

    git format-patch -3                  // 从head往前3个提交的内容,可修改为你想要的数值

    git format-patch –n 07fe          // -n指patch数,07fe对应提交的名称, 某次提交(含)之前的几次提交

    git format-patch -1 commit-id   // + commit-id 选定的那个commit打patch

     

    期间有时会遇到合并冲突,正常的原因一般是未及时下载新版本产生了冲突,特殊一点的原因是手工修改 patch 内容导致的。有时候看注释写得不够准确,忍不住就改了,有时候是 Geany 保存时自动去除了 patch 原文中的行尾空格,有时候是文件回车格式、BOM 等变动了,总之合并 patch 的时候,如果生成 patch 的“原稿”找不到,一般就产生了冲突,比如:

     

    $ git am 0001-BUG-Sybase.patch

    Applying: CHG: 读取Sybase如果时间为空,设置默认时间的修改

    error: patch failed: source.php:38

    error: source.php: patch does not apply

    Patch 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".

     

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

    $ git apply --reject 0001-BUG-Sybase.patch

    Checking patch source.php...

    error: while searching for:

            // 注释

            // 以下为几行代码片断

    error: patch failed: source.php:38

    Applying 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.php

    index 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 --resolved

    Applying: CHG: 读取Sybase如果时间为空,设置默认时间的修改

    大功告成。

    最新回复(0)