Using git on Windows you may find that if you create a patch using git format-patch
and try to apply it using git am
, the patch file will fail to apply.
The error message is "patch does not apply". If you inspect the patch file, you might be baffled
because the patch does apply, that is, the context lines in the diff exactly match (byte for byte) the file being patched.
The problem is that git am
invokes git mailsplit
to split the patch file into individual patches -- and the latter component strips carriage-return characters by default. Consequently, the patch context fails to match the target files and the patch fails.
If you use git am --ignore-space-change
to apply the patch, then it will succeed but any newly added lines will not conform to the Windows newline convention. The lines will end with a linefeed only.
A better solution is to use git am --keep-cr
. This arranges to pass the --keep-cr
option to git mailsplit
, leaving the Windows-style line-endings in place. The patch will then be applied successfully. You can make this behaviour the default through a configuration variable in the am
section of the git configuration file:
[am]
keep-cr = true