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

GitNetbeansWhitespaceRemoving WhitespaceTextwrangler

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

Are you sure those are hard errors? By default, git will warn about whitespace errors, but will still accept them. If they are hard errors then you must have changed some settings. You can use the --whitespace= flag to git apply to control this on a per-invocation basis. Try

git apply --whitespace=warn patchname.patch

That will force the default behavior, which is to warn but accept. You can also use --whitespace=nowarn to remove the warnings entirely.

The config variable that controls this is apply.whitespace.


For reference, the whitespace errors here aren't errors with your patch. It's a code style thing that git will, by default, complain about when applying patches. Notably, it dislikes trailing whitespace. Similarly git diff will highlight whitespace errors (if you're outputting to a terminal and color is on). The default behavior is to warn, but accept the patch anyway, because not every project is fanatical about whitespace.

Solution 3 - Git

Try patch -p1 < filename.patch

Solution 4 - Git

Many times i have faced such issues when my team mate works on Linux/Windows or uses git send-email.

I always used to follow below command.

git apply -3 --whitespace=fix yourpatch.patch

or

git am -s -3 --whitespace=fix yourpatch.patch

-3 option will try three-way merge and it will help to solve other issues also.

Solution 5 - Git

the one line solution is:

emacs <filename> -f delete-trailing-whitespace -f save-buffer -f kill-emacs

source: https://wiki.gnome.org/Projects/GnomeShell/Development/WorkingWithPatches

Solution 6 - Git

I think the question of how to cope with the whitespace has been adequately answered, but you asked where it came from. You mentioned ^M at the ends of lines: that’s how Git shows Windows line endings. Maybe try running dos2unix on your source files before creating patches, or use an editor which maintains the original line endings.

Solution 7 - Git

you may use combinediff with -w option from patchutils [1]

git diff | combinediff - /dev/null -w > patchname.patch

[1]
https://github.com/twaugh/patchutils
https://repology.org/project/patchutils/versions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbethView Question on Stackoverflow
Solution 1 - GitzednightView Answer on Stackoverflow
Solution 2 - GitLily BallardView Answer on Stackoverflow
Solution 3 - GitflashView Answer on Stackoverflow
Solution 4 - GitJeegar PatelView Answer on Stackoverflow
Solution 5 - GitaskalskiView Answer on Stackoverflow
Solution 6 - GitTRiGView Answer on Stackoverflow
Solution 7 - GitSérgioView Answer on Stackoverflow