61

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.

beth
  • 1,916
  • 4
  • 23
  • 39
  • You might want to give more details about your process of creating a patch. – Chronial Jan 24 '13 at 22:45
  • I added details. Not sure how helpful that will be, but it can't hurt. – beth Jan 25 '13 at 00:26
  • I am facing the same issue and have done some research. It looks like git adds these whitespaces automatically to `git diff` and `fit show`, so any patch made with such code will have trailing whitespaces. If you have linux kernel git repository locally (linus kernel tree), you can run `git show 402bae597e`. It will show you a trailing whitespace before static DEVICE_ATTR_RO(flags); and the closing braces in previous line, even though they are not present in the code. – Hamzahfrq Sep 11 '15 at 11:01
  • @Chronial This problem can be reproduced by adding a line to start or beginning of a method (where you have something written in the beginning of a line e.g. opening/closing brace or function prototype). Then run `git diff`. You can see extra whitespaces before unchanged files. These then get introduced to the patch when `git format-patch` is used – Hamzahfrq Sep 11 '15 at 11:04

8 Answers8

56

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

Darian Lewin commented:

Useful to automatically fix whitespace errors. --whitespace=fix ensures whitespace errors are fixed before path is applied, and --reject ensures atomicity so no working directory files are modified if patch will not apply.

Source: git-scm.com/docs/git-apply

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
pinkRhino
  • 1,164
  • 9
  • 11
  • 9
    Useful to automatically fix whitespace errors. --whitespace=fix ensures whitespace errors are fixed before path is applied, and --reject ensures atomicity so no working directory files are modified if patch will not apply. Source: https://git-scm.com/docs/git-apply – Darian Lewin Feb 21 '17 at 09:41
48

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.

S M
  • 8,155
  • 3
  • 35
  • 36
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 4
    I understand that. However, I am confused as to where this whitespace is coming from. I didn't ask whether I should care about it, I asked how to fix it. – beth Jan 25 '13 at 02:31
  • 2
    The whitespace is coming from the patch you're trying to apply. Which means it came from the diff you originally took. – Lily Ballard Jan 25 '13 at 05:43
  • 2
    I know that. I am trying to find out how to create a diff without ending whitespace in it. – beth Jan 25 '13 at 16:05
  • 7
    @beth: By, uh, removing the trailing whitespace in your source before creating the diff? Or you could just go ahead and use `git apply --whitespace=fix patchname.patch`, which will fix the whitespace errors it sees for you. – Lily Ballard Jan 25 '13 at 19:52
  • Okay, just imagine that it is important to me not to have trailing whitespace at all. I mentioned attempting to remove it in several different editors. "By removing trailing whitespace" isn't really an answer to the question "how do I remove trailing whitespace". Sorry if I was unclear. – beth Jan 25 '13 at 20:18
  • 5
    @beth: You mentioned trying to remove it from the diff. Manually editing diffs is a tricky process. But again, you can use `git apply --whitespace=fix patchname.patch` and git will fix the whitespace for you. – Lily Ballard Jan 25 '13 at 20:34
  • White space was at on the very last line but it didn't fix it – alper Jan 07 '21 at 20:08
9

Try patch -p1 < filename.patch

flash
  • 1,231
  • 4
  • 19
  • 23
  • 3
    What does this do? Where does the .diff file come from? – beth Mar 21 '14 at 16:19
  • its just a patch file – flash Mar 27 '14 at 17:21
  • I think your carat is backwards or something is missing. – beth Mar 27 '14 at 18:24
  • The patch command doesn't tell git that files have been added or removed. The suggestion by zednight (git apply --reject --whitespace=fix mychanges.path) is better, because it deals with added and removed files properly. – Erwin Coumans Nov 17 '15 at 16:20
  • The original sign (`<`) **is** correct, this way the destination files are determined by the patch file itself. See `man patch`: _patch [options] [originalfile [patchfile]]_ but usually just _patch -pnum – Martin R. May 14 '18 at 16:56
  • Please add more details to your answer. – Jalal Jul 24 '21 at 13:07
6

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.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
3

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

askalski
  • 43
  • 5
2

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.

TRiG
  • 10,148
  • 7
  • 57
  • 107
0

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

Sérgio
  • 6,966
  • 1
  • 48
  • 53
0

You can try running fromdos command to fix whitespace errors. Basically, it'll convert the diff file from DOS to Unix format. So, you just need to run:

fromdos mychanges.patch

Then, if you run:

git apply mychanges.patch

there'll be no trailing whitespace errors.

Edit: I think user TRig suggested the same thing, fromdos unix command is an alternative to the suggestion of using dos2unix.