Git打补丁常见问题

Git打补丁常见问题

 

  往往觉得得到某个功能的补丁就认为这个功能我就已经成功拥有了,但是在最后一步的打补丁的工作也是需要相当谨慎的,甚至有可能还要比你获取这个补丁花费的时间还要多。看到好多同行遇到这个问题,且最近自己也花费近20天「获取,打,验证」一个特性功能的补丁。趁热总结一下,知识点可能不多,但是问题是相当棘手的。

 

 

$ git apply -h

usage: git apply [options] [<patch>...]

 

    --exclude <path>      don't apply changes matching the given path

    --include <path>      apply changes matching the given path

    -p <num>              remove <num> leading slashes from traditional diff paths

    --no-add              ignore additions made by the patch

    --stat                instead of applying the patch, output diffstat for the input

    --numstat             show number of added and deleted lines in decimal notation

    --summary             instead of applying the patch, output a summary for the input

    --check               instead of applying the patch, see if the patch is applicable

    --index               make sure the patch is applicable to the current index

    --cached              apply a patch without touching the working tree

    --apply               also apply the patch (use with --stat/--summary/--check)

    -3, --3way            attempt three-way merge if a patch does not apply

    --build-fake-ancestor <file>

                          build a temporary index based on embedded index information

    -z                    paths are separated with NUL character

    -C <n>                ensure at least <n> lines of context match

    --whitespace <action>

                          detect new or modified lines that have whitespace errors

    --ignore-space-change

                          ignore changes in whitespace when finding context

    --ignore-whitespace   ignore changes in whitespace when finding context

    -R, --reverse         apply the patch in reverse

    --unidiff-zero        don't expect at least one line of context

    --reject              leave the rejected hunks in corresponding *.rej files

    --allow-overlap       allow overlapping hunks

    -v, --verbose         be verbose

    --inaccurate-eof      tolerate incorrectly detected missing new-line at the end of file

    --recount             do not trust the line counts in the hunk headers

    --directory <root>    prepend <root> to all filenames

 

 

第一步检测补丁有无问题

$ git apply --check xxx.patch

能检测出现的问题有以下几种例子:

 

1. error: cannot apply binary patch to 'xxx' without full index line

xxx一般会是bin/png/gif等等二进制文件 具体的原因就是patch中有指明要打上xxx文件,但是这个文件并不包含在这个patch中,仅仅是有一个名字存在其中。遇到这个问题要重视。

 

2. error: core/java/android/provider/Settings.java: patch does not apply

出现这种一般会是补丁冲突,这种一般是强制打上补丁(使用--reject)后根据产生的*.rej文件来手动解决冲突。

 

3. warning: core/java/android/view/View.java has type 100644, expected 100755

出现这种警告一般是文件内没有冲突,但是文件的权限发生变动。一般没有影响。

 

 

第二步强制打补丁

$ git apply --reject xxx.patch

执行了这一步后会产生什么样的结果,我对第一步的冲突来对应说明。

 

1.这种问题一般是制作补丁的开发人员没有将二进制文件制作到patch中云,对于这种情况不会有任何的提示,因为patch中源资源文件都没有,Git也没有什么招术来解决。最好的方法是联系补丁提供者。

 

2.这种情况是由于git apply是对比补丁中前后几行代码,如果没有出现在目标文件中,那么这就是冲突。这个是比较经常出现的,对于这种情况会生成*.rej文件,可以find ./ -name *.rej找到这些冲突的补丁,手动打上就好。

 

3.可以考虑忽略。

 

目前就这些,遇到新的问题再补充。


git am -3 -k后如果有冲突,不要执行git checkout。如果不愿意修改冲突文件,佯装修改一下,添加进去才能进行下一步。
git --git-dir=../other_proj_dir/.git format-patch -k -1 --stdout xxxxxxxxxxxxxxxxxx | git am -3 -k
git am同样有--reject选项,添加这个选项可以将能打上的补丁先打上,冲突的文件生成*.rej文件。

  • 8
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Git中,我们有几种方法可以打补丁(patch)。其中两种常用的方法是使用git am命令和git apply命令。 使用git am命令可以在打补丁的同时进行commit,因此不需要重新commit。你可以使用以下命令来打补丁: - 单个补丁文件:git am 0001-xxx.patch - 某个文件路径下的所有补丁文件:git am filepath/*.patch 而使用git apply命令则需要重新commit。你可以使用以下命令来打补丁: - 检查补丁文件:git apply --stat xxx.patch (xxx.diff) - 检查补丁能否应用成功:git apply --check xxx.patch (xxx.diff) - 打补丁git apply xxx.patch (xxx.diff) 此外,你还可以使用git format-patch命令生成只适用于Git的patch文件,该文件包含diff信息、提交人、提交时间等。如果使用git format-patch生成的补丁无法打到当前分支,git am命令会给出相应的提示并帮助你完成打补丁的工作。 请注意,以上都是使用Git命令来进行补丁操作的方法。你可以参考官方文档来获取更详细的使用说明和参数解释 。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Patch和Git打补丁学习笔记](https://blog.csdn.net/m0_37221216/article/details/105334100)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁保康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值