git 如何使用 diff 创建和正确应用 .patch 文件到单个 .cpp 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7138131/
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 05:52:32  来源:igfitidea点击:

How to create and apply properly .patch file to a single .cpp file using diff?

gitdiffpatchline-endings

提问by Placeholder

I'm trying to apply a .patchfile to a single .cppfile using git diff.

我正在尝试使用git diff.patch文件应用于单个.cpp文件。

These are my files: old.cpp, new.cppand fix.patch.

这些是我的文件:old.cppnew.cppfix.patch

old.cppis the original unmodified source code, new.cppis the modified source and fix.patchis the patch I want to create which when applied to old.cppshould apply the changes from new.cppto it. Both old.cppand new.cppare with Windows (CR LF)line endings, both files are 918 KBlarge, only one line is being modified in the source.

old.cpp是原始未修改的源代码,new.cpp是修改源和fix.patch是我要创建补丁,当应用于old.cpp应该适用从变化new.cpp它。既old.cppnew.cpp视窗(CR LF)行结尾,这两个文件是918 KB大,只有一行被在源修改。

I create the patch by putting the two files old.cppand new.cppin the same folder and using Git Bashprompt with the command:

我通过将两个文件old.cppnew.cpp放在同一文件夹中并使用Git Bash提示符和命令来创建补丁:

git diff -u old.cpp new.cpp > fix.patch

The fix.patchfile successfully appears but when I actually test it and apply it to old.cppwith Git Bashby typing:

fix.patch文件成功出现,但是当我实际测试,并运用它来old.cpp混帐猛砸通过键入:

patch old.cpp fix.patch

the patch is being applied successfully but the size of old.cppreducesfrom 918 KBto 894 KB. After some research with kdiff3I found that my newly created fix.patchfile is with Unix (LF)line endings and after applying it to old.cpp, the patched old.cppis adopting Unix (LF)line endings too. I guess this is the reason why the file size of old.cppreduces too.

补丁已成功应用,但old.cpp的大小从918 KB减少894 KB。在对kdiff3进行一些研究后,我发现我新创建的fix.patch文件使用Unix (LF)行结尾,并将其应用于old.cpp 后,修补的old.cpp也采用了Unix (LF)行结尾。我想这就是old.cpp的文件大小减小的原因。

My question is what commandshould I use in gitor what else I have to do so my newly created fix.patchfile remains with Windows (CR LF)line endings and after applying the patch to old.cppthe newly patched old.cppfile is with Windows (CR LF)line endings too and the file size does not reduce so drastically. I received a suggestion here to use git applyinstead of patchbut I don't know what exactly to type after git applyso it works the way I want for my situation. :(

我的问题是我应该在git 中使用什么命令或者我还必须做什么,以便我新创建的fix.patch文件保留在Windows (CR LF)行尾,并且在将补丁应用于old.cpp 后,新修补的old.cpp文件也有Windows (CR LF)行结尾,文件大小不会大幅减少。我在这里收到了一个使用而不是的建议,但我不知道之后到底要输入什么,所以它按照我想要的方式工作。:(git applypatchgit apply

I am using Windows XP SP2, Git 1.7.6, Git Extensions 2.24and Microsoft Visual C++ 2010.

我使用的是Windows XP SP2Git 1.7.6Git Extensions 2.24Microsoft Visual C++ 2010

回答by Placeholder

OK, I finally solved the issue and learned my lesson aswell. :)

好的,我终于解决了这个问题,也吸取了教训。:)

The patch are created with Git Bashcommand prompt:

该补丁是使用Git Bash命令提示符创建的:

git diff -u old.cpp new.cpp > fix.patch

Then make sure the old.cppand fix.patchare in the same folderthe header of fix.patchreads:

然后确保old.cppfix.patch都在同一个文件夹的标题fix.patch写着:

diff --git a/old.cpp b/old.cpp
index 04784e1..da68766 100644
--- a/old.cpp
+++ b/oldcpp

After that type in Git Bashprompt:

之后输入Git Bash提示符:

git apply fix.patch

And voila! No line messups, errors or problems. I hope this helps someone who stumbles open the same thing some day Thanks to everyone for the fast and deep replies, they helped me learn something new today. :)

瞧!没有线路混乱、错误或问题。我希望这能帮助那些有一天偶然打开同样东西的人感谢大家快速而深刻的回复,他们帮助我今天学到了一些新东西。:)

回答by patthoyts

Try applying the patch using 'git apply' instead of 'patch'. git apply will call patch but should obey any other git directives which might sort your crlf issue here. It also accepts patch options (like 'git apply -p1' for pruning path elements). I quite often also use 'git apply' to apply patches from git to CVS trees. 'git apply' doesn't require that the target be a git repository.

尝试使用“git apply”而不是“patch”来应用补丁。git apply 将调用 patch 但应该遵守任何其他 git 指令,这些指令可能会在这里对您的 crlf 问题进行排序。它还接受补丁选项(如用于修剪路径元素的“git apply -p1”)。我也经常使用 'git apply' 将补丁从 git 应用到 CVS 树。'git apply' 不要求目标是 git 存储库。

回答by Noobie

Use --includeflag.

使用--include标志。

git apply patch.patch --include=new.cpp