zoukankan      html  css  js  c++  java
  • git diff,git format-patch,git apply和patch小问题三则

    git diff,git format-patch,git apply和patch小问题三则

    转载自:https://blog.csdn.net/lezhang123/article/details/55095935

    Patch文件有3中,git 给我们提供了2种patch 方案, 一是用git diff生成的标准patch,二是git format-patch生成的Git专用Patch。Linux 本身也有一个patch命令.

    git diff

    使用git diff 来输出2个commit 之前的不同之处来生成patch.

    生成的这个patch文件就是我们修改的内容

    git format-patch

    1.将最新的一条修改记录生成patch

     

    使用git format-patch生成的patch在文件的前面会有diff的信息,还有提交者,时间等等,仔细一看你会发现,这是个E-mail的文件,你可以直接发送它!这种patch,我们要用git am来应用。

    如何直接把其他仓库的修改记录直接合入到当前仓库

    例子:

    git --git-dir=/home/builder2010/work/6589kk/ALPS.KK1.MP5.V1.3_EASTAEON89_WET_KK/.git format-patch -k -1 --stdout 57b1ace1| git am -3 -k

    Git diff 和 git format-patch的对比.

    A.兼容性:很明显,git diff生成的Patch兼容性强。如果你在修改的代码的官方版本库不是Git管理的版本库,那么你必须使用git diff生成的patch才能让你的代码被项目的维护人接受。

    B.除错功能:对于git diff生成的patch,你可以用git apply --check 查看补丁是否能够干净顺利地应用到当前分支中;如果git format-patch 生成的补丁不能打到当前分支,git am会给出提示,并协助你完成打补丁工作,你也可以使用git am -3进行三方合并,详细的做法可以参考git手册或者《Progit》。从这一点上看,两者除错功能都很强。

    C.版本库信息:由于git format-patch生成的补丁中含有这个补丁开发者的名字,因此在应用补丁时,这个名字会被记录进版本库,显然,这样做是恰当的。因此,目前使用Git的开源社区往往建议大家使用format-patch生成补丁。

    git apply

    git apply 可以应用使用git diff 和git format-patch生成的2种patch来打补丁.

    使用git apply 命令之后patch文件中的修改会自动合入到对应的文件中,但是不会帮我们自动提交这个commit.

    android1@Ubuntu:alps$ git apply 0001-mp3.patch

    alps/0001-mp3.patch:30: trailing whitespace.

             //zhangle add start

    alps/0001-mp3.patch:32: space before tab in indent.

                   mimetype = "application/vnd.android.package-archive";

    warning: alps/packages/apps/Browser/src/com/android/browser/DownloadHandler.java has type 100755, expected 100644

    warning: 2 lines add whitespace errors.

    这里只有警告,表示patch已经成功应用了,可以使用git status来查看工作区的文件状态

    android1@Ubuntu:alps$ git status

    # On branch Android_S510_AI-XL_no_codec

    # Changes not staged for commit:

    #(use "git add <file>..." to update what will be committed)

    #(use "git checkout -- <file>..." to discard changes in working directory)

    #

    #       modified:packages/apps/Browser/src/com/android/browser/DownloadHandler.java

    #       modified:packages/providers/DownloadProvider/src/com/android/providers/downloads/OpenHelper.java

    #

    # Untracked files:

    #(use "git add <file>..." to include in what will be committed)

    #

    #       0001-mp3.patch

    no changes added to commit (use "git add" and/or "git commit -a")

    如果没有什么冲突提示就直接使用git commit提交就可以了

    如果遇到git apply的空格相关的问题,可以使用—whitespace 参数来尝试解决问题.

    android1@Ubuntu:alps$ git apply --whitespace=warn 0001-mp3.patch

    git am

    android1@Ubuntu:alps$ git am 0001-mp3.patch

    git am 会直接使用patch文件中的diff的信息,还有提交者,时间等等来自动提交,不需要我们再去提交commit

     

    git am 必须使用的是用git format-patch 生成的patch文件来打补丁,而不能是使用git diff生成的patch.如果使用的是git diff生成的patch,会出现下面这个错误.

    android1@Ubuntu:alps$ git am patch2.patch

    Patch format detection failed.

    使用补丁维护git仓库时遇到的小问题

    1、包含二进制文件时的diff和apply

    foo$ git diff HEAD^..HEAD > foobar.patch

    bar$ git apply foobar.patch

    ...

    error: cannot apply binary patch to 'root/images/back_disabled.png' without full index line

    error: root/images/back_disabled.png: patch does not apply

    ...

    git apply提示错误,无法应用补丁。是因为普通的diff格式文件是不支持二进制文件的,新增的或者发生变化的二进制文件无法在diff文件中体现。git扩展了diff使其支持二进制格式,生成补丁时指定选项--binary即可,生成的文件可以顺利的git apply。

    $ git diff HEAD^..HEAD --binary > foobar.patch

    2、git apply的空白问题

    $ git apply foobar.patch

    foobar.patch:271: trailing whitespace.

    foobar.patch:465: space before tab in indent.

    .paging_full_numbers a.paginate_active {

    warning: squelched 1705 whitespace errors

    warning: 1710 lines add whitespace errors.

    看看git apply(1)手册上怎么说:

    – -whitespace=

    When applying a patch, detect a new or modified line that has whitespace errors. What are considered whitespace errors is controlled by core.whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors.

    By default, the command outputs warning messages but applies the patch. When git-apply is used for statistics and not applying a patch, it defaults to nowarn.

    You can use different values to control this behavior:

    nowarn — turns off the trailing whitespace warning.

    warn — outputs warnings for a few such errors, but applies the patch as-is (default).

    fix — outputs warnings for a few such errors, and applies the patch after fixing them (strip is a synonym — the tool used to consider only trailing whitespace characters as errors, and the fix involved stripping them, but modern gits do more).

    error — outputs warnings for a few such errors, and refuses to apply the patch.

    error-all — is similar to error but shows all errors.

    git apply应用补丁时会检测空白错误,默认情况下,尾部空白,包含空白的空行,初始tab缩进之后紧跟的空白字符会被认为是错误。

    处理这个错误的行为由命令行参数--whitespace或者core.whitespace配置来控制,共有5种可能的动作:

    nowarn

    关闭错误提示

    warn

    输出部分错误提示,但完整的应用补丁,不会处理错误,这是默认动作。

    fix

    输出部分错误,修正错误后应用补丁

    error

    输出部分错误,拒绝应用补丁。

    error-all

    输出全部的错误,拒绝应用补丁。

    3、patch命令无法处理含有空白的文件名

    $ patch p1 < foobar.patch

    can't find file to patch at input line 716

    Perhaps you used the wrong -p or --strip option?

    The text leading up to this was:

    --------------------------

    |diff --git a/root/images/Sorting icons.psd b/root/images/Sorting icons.psd

    |new file mode 100644

    |index 0000000..53b2e06

    |Binary files /dev/null and b/root/images/Sorting icons.psd differ

    --------------------------

    File to patch:

    Skip this patch? [y]

    因为diff文件中的文件名含有空格,patch命令无法应用这样的diff补丁。应该避免使用带有特殊字符的文件名。

    当然patch也无法引用二进制diff补丁,使用普通diff格式生成含有二进制文件的补丁时,patch会应用成功,但生成的二进制文件是空的。

    4.错误: previous rebase directory .git/rebase-apply still exists but mbox given.

    先执行git am –abort 在执行git am --whitespace=warn 0001-.patch

  • 相关阅读:
    Matlab 绘图实例
    最大回撤,最大回撤恢复时间与最大回撤持续期
    scikit-learn 中 OneHotEncoder 解析
    TensorFlow 内置重要函数解析
    关于字符串常用的函数
    SQL 值得记住的点
    Ubuntu E: 无法获得锁 /var/lib/dpkg/lock
    Ubuntu 16.04 同时使用python3.5
    numpy
    Jupyter notebook 使用Turorial
  • 原文地址:https://www.cnblogs.com/cqx6388/p/14436824.html
Copyright © 2011-2022 走看看