My diff contains trailing whitespace - how to get rid of it?

beth picture beth · Jan 24, 2013 · Viewed 68.2k times · Source

I've tried editing a php file in TextWrangler with line endings set to Unix, in NetBeans, and in vim. When I save the diff to a patch and then try to apply it, it gives whitespace errors. When I type git diff I can see ^M at the ends of my lines, but if I manually remove these in vim, it says my patch file is corrupted, and then the patch doesn't apply at all.

I create a patch with the following command:

git diff > patchname.patch

And I apply it by checking out a clean version of the file to be patched and typing

git apply patchname.patch

How can I create this patch without whitespace errors? I've created patches before and never run into this issue.

Answer

zednight picture zednight · Dec 9, 2014

git apply --reject --whitespace=fix mychanges.path