When creating a diff patch with Git Shell in Windows (when using GitHub for Windows), the character encoding of the patch will be UCS-2 Little Endian according to Notepad++ (see the screenshots below).
How can I change this behavior, and force git to create patches with ANSI or UTF-8 without BOM character encoding?
It causes a problem because UCS-2 Little Endian encoded patches can not be applied, I have to manually convert it to ANSI. If I don’t, I get “fatal: unrecognized input” error.
Since then, I also realized that I have to manually convert the EOL from Windows format (\r\n) to UNIX (\n) in Notepad++ (Edit > EOL Conversion > UNIX). If I don’t do this, I get “trailing whitespace” error (even if all the whitespaces are trimmed: “TextFX” > “TextFX Edit” > “Trim Trailing Spaces”).
So, the steps I need to do for the patch to be applied:
- create patch (here is the result)
- convert character encoding to ANSI
- EOL conversion to UNIX format
- apply patch
Please, take a look at this screenshot:



I’m not a Windows user, so take my answer with a grain of salt. According to the Windows PowerShell Cookbook, PowerShell preprocesses the output of
git diff, splitting it in lines. Documentation of theOut-FileCmdlet suggests, that>is the same as| Out-Filewithout parameters. We also find this comment in the PowerShell documentation:So, apparently it is not Git which chooses the character encoding, but
Out-File. This suggests a) that PowerShell redirection really should only be used for text and b) thatwill avoid the encoding problems. However, this still does not solve the problem with Windows vs. Unix line-endings . There are Cmdlets (see the PowerShell Community Extensions) to do conversion of line-endings.
However, all this recoding does not increase my confidence in a patch (which has no encoding itself, but is just a string of bytes). The aforementioned Cookbook contains a script Invoke-BinaryProcess, which can be used redirect the output of a command unmodified.
To sidestep this whole issue, an alternative would be to use
git format-patchinstead ofgit diff.format-patchwrites directly to a file (and not to stdout), so its output is not recoded. However, it can only create patches from commits, not arbitrary diffs.format-patchtakes a commit range (e.g.master^10..master^5) or a single commit (e.g. X, meaning X..HEAD) and creates patch files of the form NNNN-SUBJECT.patch, where NNNN is an increasing 4-digit number and subject is the (mangled) subject of the patch. An output directory can be specified with-o.