I'm trying to apply a .patch file to a single .cpp file using git diff.
These are my files: old.cpp , new.cpp and fix.patch.
old.cpp is the original unmodified source code, new.cpp is the modified source and fix.patch is the patch I want to create which when applied to old.cpp should apply the changes from new.cpp to it. Both old.cpp and new.cpp are with Windows (CR LF) line endings, both files are 918 KB large, only one line is being modified in the source.
I create the patch by putting the two files old.cpp and new.cpp in the same folder and using Git Bash prompt with the command:
git diff -u old.cpp new.cpp > fix.patch
The fix.patch file successfully appears but when I actually test it and apply it to old.cpp with Git Bash by typing:
patch old.cpp fix.patch
the patch is being applied successfully but the size of old.cpp reduces from 918 KB to 894 KB. After some research with kdiff3 I found that my newly created fix.patch file is with Unix (LF) line endings and after applying it to old.cpp, the patched old.cpp is adopting Unix (LF) line endings too. I guess this is the reason why the file size of old.cpp reduces too.
My question is what command should I use in git or what else I have to do so my newly created fix.patch file remains with Windows (CR LF) line endings and after applying the patch to old.cpp the newly patched old.cpp file 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 apply
instead of patch
but I don't know what exactly to type after git apply
so it works the way I want for my situation. :(
I am using Windows XP SP2, Git 1.7.6, Git Extensions 2.24 and Microsoft Visual C++ 2010.
OK, I finally solved the issue and learned my lesson aswell. :)
The patch are created with Git Bash command prompt:
git diff -u old.cpp new.cpp > fix.patch
Then make sure the old.cpp and fix.patch are in the same folder the header of fix.patch reads:
diff --git a/old.cpp b/old.cpp
index 04784e1..da68766 100644
--- a/old.cpp
+++ b/oldcpp
After that type in Git Bash prompt:
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. :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With