如何在git中应用补丁文件

9
如何在git本地仓库中应用补丁?
我尝试了以下两种方法:
方法一: $ git am < 0001-Add-Voicemail-tab-to-Contacts.patch 但是出现错误信息:Patch does not have a valid e-mail address.
方法二: $ git apply 0001-my.patch 但是出现错误信息:fatal: git diff header lacks filename information (line 27),其中第27行是我的补丁文件中的“GIT binary patch”,该文件中包含一个png文件。
请注意,以上内容保留了html标签,如需使用请自行删除。
diff --git a/res/drawable-finger/icl.png b/res/drawable-finger/icl.png
new file mode 100644
index 0000000000000000000000000000000000000000..f78e65cf94d22059e0caeb90caee03e17166f109
GIT binary patch
literal 1697
zcmV;S244AzP)<h;3K|Lk000e1NJLTq001BW001Be1^@s6b9#F80000PbVXQnQ*UN;
zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU&LrFwIRCwCNS6ygRXBd9-pOdCZnl?Wr
zT}!*JMXxLc!LeIm!`%pu!FDwmis0~S-pnf*2)mHMWH7G;(JNtBW1={AA=C-BqUf{=
1个回答

19

我通过模拟行尾问题,成功地复现了你的问题。

$ cp /bin/ls .
$ git add ls; git commit -m second
[master 8668716] second
 1 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 ls
$ git format-patch HEAD^..HEAD
0001-second.patch
$ git reset --hard HEAD^
HEAD is now at 686ace7 first
$ unix2dos 0001-second.patch
$ git apply 0001-second.patch
fatal: git diff header lacks filename information (line 14)

假设你正在使用Linux系统,尝试执行以下命令:

Assuming you're running Linux, try

$ dos2unix 0001-Add-Voicemail-tab-to-Contacts.patch
$ git apply !$

如果你正在运行Cygwin,执行相反的转换:

$ unix2dos 0001-Add-Voicemail-tab-to-Contacts.patch
$ git apply !$

我认为这与补丁中的二进制文件有关。 - n179911
我能够拿到一个可用的补丁并引发你看到的错误。请查看编辑后的答案。 - Greg Bacon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接