git 如何应用 .diff 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21274840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:37:40  来源:igfitidea点击:

How to apply a .diff file

gitmergediffpatch

提问by chengpei

I got a .diff type file , seems like blew:

我得到了一个 .diff 类型的文件,看起来像是爆炸了:

diff --git a/res/User.lua b/res/User.lua
index db8c2cc..4d2af0f 100644
--- a/res/User.lua
+++ b/res/User.lua
@@ -5,6 +5,7 @@ resetPassword = {}
+UserInfo = {}

Should i manually modify my local User.lua , or can i apply a diff file as like apply a patch file ? (or should convert a .diff file to .patch file, how to?)

我应该手动修改我的本地 User.lua ,还是可以像应用补丁文件一样应用 diff 文件?(或者应该将 .diff 文件转换为 .patch 文件,如何?)

would be thankful for any help.

将不胜感激任何帮助。

回答by VonC

should convert a .difffile to .patchfile, how to?

应该将.diff文件转换为.patch文件,如何转换?

No, the extension isn't important. The content is.

不,扩展名并不重要。内容是。

You can try, and if doesn't work, fallback on this commentby Евгений Чорба (Evgeny Solis):

你可以试试,如果不能正常工作,后备对此有何评论ЕвгенийЧорба(叶夫根尼·索利斯)

For those who has no patch command and git applydoes nothing. The solution is:
Let's modify the patch file!

From

对于那些没有补丁命令并且git apply什么都不做的人。解决办法是:
我们修改补丁文件!

diff --git a/uc_attribute/uc_attribute.admin.inc b/uc_attribute/uc_attribute.admin.inc
index b9a978a..ef33ca3 100644
--- a/uc_attribute/uc_attribute.admin.inc
+++ b/uc_attribute/uc_attribute.admin.inc

To:

到:

diff --git ubercart/uc_attribute/uc_attribute.admin.inc ubercart/uc_attribute/uc_attribute.admin.inc
index 1c35bf8..587fa67 100755
--- ubercart/uc_attribute/uc_attribute.admin.inc
+++ ubercart/uc_attribute/uc_attribute.admin.inc

Apply patch from project root using

git apply -p0 PATCHFILE.patch

Verbose:
You should replace 'a/' and 'b/' from patchfile with '<projectname>/' (In my example it is 'ubercart')

After applying patch you may see warning like

使用从项目根应用补丁

git apply -p0 PATCHFILE.patch

详细:
您应该将补丁文件中的' a/' 和 ' b/'替换为 ' <projectname>/'(在我的示例中是 ' ubercart')

应用补丁后,您可能会看到类似警告

warning: ubercart/uc_attribute/uc_attribute.admin.inc has type 100755, expected 100644

It's OK, don't worry.

NOTE! If patch contains diffs for a several files you should replace ALL occurrences of 'a/<anypath>' and 'b/<anypath>'

没事不用担心。

笔记!如果补丁包含多个文件的差异,您应该替换所有出现的“ a/<anypath>”和“ b/<anypath>



Note: the OP chengpeihas seen the other error messagewhen using git apply:

注:OP chengpei已经看到了其他错误消息当使用git apply

  got a error: fatal: corrupt patch at line 7

It is documented in "fatal: corrupt patch at line XX” when staging single line"

暂存单行时记录在“ ”中fatal: corrupt patch at line XX

having newlines at the end of the file fixes it, and removing them causes it.

在文件末尾有换行符可以修复它,删除它们会导致它。



Magnus B?ckrecommends in the comments:

Magnus B?ck在评论中推荐:

Instead of editing the patch file to remove directory prefixes a/and b/, run patch -p1to have the first directory component stripped automatically.

不要编辑补丁文件来删除目录前缀a/b/,而是运行patch -p1以自动剥离第一个目录组件。

trembyadds in the comments:

tremby在评论中补充道:

To produce a diff from git without the a/and b/prefixes you can use --no-prefixas an option to git diff

要在没有a/b/前缀的情况下从 git 生成差异,您可以将其--no-prefix用作选项git diff