Day: April 14, 2017

How to remove trailing whitespaces and new blank line at EOF from patches

When applying a patch in the git (i.e. using “git am” command) you could to face these warning messages:

Applying: You patch name
.git/rebase-apply/patch:9360: trailing whitespace.
 *    notice, this list of conditions and the following disclaimer. 
.git/rebase-apply/patch:2456: new blank line at EOF.
+
.git/rebase-apply/patch:3382: new blank line at EOF.
+
.git/rebase-apply/patch:4323: new blank line at EOF.
+
.git/rebase-apply/patch:4905: new blank line at EOF.
+

It will be very boring to discover what are these files and fix each one.
Fortunately you can use git itself to fix it for you:

$ git apply --reject --whitespace=fix filename.patch

If you want you can use “git add” and “git commit” to create a new patch without these “trailing whitespaces” and “new blank lines”.

UPDATE: You can use “git am –reject –whitespace=fix filename.patch” then it will fix the blank lines and trailing whitespaces and you don’t need to run “git add” and “git commit”!

I found the solution here: http://stackoverflow.com/questions/14509950/my-diff-contains-trailing-whitespace-how-to-get-rid-of-it

Advertisements