zoukankan      html  css  js  c++  java
  • 如何使用git 生成patch 和打入patch【转】

    本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361

    平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用git 生成patch 给到我们,那我们就需要把客户给到patch 打入到我们的project ,基于这样一个场景,我把git 如何生成patch 和如何打入patch 做总结

     

    生成patch 的方法:(我这里描述的生成patch 是根据commit 记录生成的)

    1.例如首先先通过git log 查看有哪一些commit


     

    2.把第一次commit 提交以后的(不包括第一次提交)都生成patch 

    如上图所示:使用命令:git format-patche795fefabc

    然后生成的patch 文件如下图所示


     

    打入patch 的方法:

    把生成的patch 文件copy 到一个文件夹中来(这里我创建了patch 文件夹)。如下图所示

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

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

    git am 可以一次合并一个文件,或者一个目录下所有的patch,如下图所示:

    ================------------------------=============--------------=================

    1 使用git format-patch生成所需要的patch:
    当前分支所有超前master的提交:
    git format-patch -M master
    某次提交以后的所有patch:
    git format-patch 4e16 --4e16指的是commit名
    从根到指定提交的所有patch:
    git format-patch --root 4e16
    某两次提交之间的所有patch:
    git format-patch 365a..4e16 --365a和4e16分别对应两次提交的名称
    某次提交(含)之前的几次提交:
    git format-patch –n 07fe --n指patch数,07fe对应提交的名称
    故,单次提交即为:
    git format-patch -1 07fe
    git format-patch生成的补丁文件默认从1开始顺序编号,并使用对应提交信息中的第一行作为文件名。如果使用了-- numbered-files选项,则文件名只有编号,不包含提交信息;如果指定了--stdout选项,可指定输出位置,如当所有patch输出到一个文件;可指定-o <dir>指定patch的存放目录;


    2应用patch:
    先检查patch文件:git apply --stat newpatch.patch
    检查能否应用成功:git apply --check newpatch.patch
    打补丁:git am --signoff < newpatch.patch

    (使用-s或--signoff选项,可以commit信息中加入Signed-off-by信息)

    如果应用patch出现问题:

    比如,一个典型的git am失败,可能是这样的:
    $ git am PATCH
    Applying: PACTH DESCRIPTION
    error: patch failed: file.c:137
    error: file.c: patch does not apply
    error: patch failed: Makefile:24
    error: libavfilter/Makefile: patch does not apply
    Patch failed at 0001 PATCH DESCRIPTION
    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只是输出上述信息,然后就停下来。一个小冲突会导致整个patch都不会被集成。
     
    处理这种问题的最简单方法是先使用 git am --abort,然后手动的添加此patch, patch -p1 < PATCH,手动解决掉代码冲突,最后使用 git commit -a 提交代码。但是这样做有个问题就是你会失去PATCH中原本包含的commit信息(比如From,Date,Subject,Signed-off-by等)。应该有一种更聪明的方法。
     
    在 .git/rebase-apply 目录下,存放着相应的补丁文件,名字是“0001” (在更新的git版本中,存放补丁文件的目录名有所改变,这里使用的git版本是 1.7.4.1)。
     
    事实上,你可以使用 git apply 命令打patch(git apply 是git中的patch命令)。如同使用 patch -p1 命令时一样,然后手动解决代码冲突(检视生成的 .rej 文件,与冲突文件比较,修改冲突内容,并最终把文件加入到index中):
     
    $ git apply PATCH --reject
    $ edit edit edit
    (译注:根据.rej文件手动解决所有冲突)
    $ git add FIXED_FILES
    $ git am --resolved
     
    就这么简单!
    想多一些解释,好吧。git am 并不改变index,你需要使用 git apply --reject 打patch(保存在 .git/rebase-apply),手动解决代码冲突,(译注:使用 git status 列出所有涉及文件),把所有文件(不仅仅是引起冲突的文件)添加到(git add)index,最后告诉 git am 你已经解决(--resolved)了问题。这样做的好处是你不需要重新编辑commit信息。而且,如果你正在打的是一系列patch(就是说你在打的是多个patch,比如 git am *.patch)你不需要使用 git am --abort,然后又 git am。

    参考资料:

    Git-format-patch(1) - Linux man page http://linux.die.net/man/1/git-format-patch

    How to create and apply a patch with Git http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git

  • 相关阅读:
    dev c++ 字符间隔大 build with c++11
    What does -> mean in Python function definitions?
    input something to stdin, especially there is "$" in the uname
    control gpu memory
    调研打标(标签)的实现方式
    分页设计思考
    模糊查询的几种实现方式
    Error creating bean with name '***': Injection of resource dependencies failed,Bean named 'redisService' is expected to be of type
    Could not write JSON: No serializer found for class
    神奇BUG
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/8241899.html
Copyright © 2011-2022 走看看