All GIT patches I create throw fatal: unrecognized input

There is format problem in patch file. To fixthe path file:

  1. Open your patch file in notepad++ then enter these two menus:

    Encoding/Convert to UTF-8
    Edit/EOL conversion/Unix (LF)
    
  2. Run:

    git apply --reject --whitespace=fix your_patch.patch
    

Updated

You might have a file which was not encoded to UTF-8. To fix that on *nix systems (MacOS, Linux etc.)

iconv -f ascii -t utf-8 fix.patch -o fix_utf8.patch

For windows you can try:

Get-Content .\fix.patch | Set-Content -Encoding utf8 fix_utf8.patch

If your file may already have color codes in it you can try:

git apply --reject --whitespace myfile.patch

Passing in color param seems to fix the problem.

git diff HEAD --color=never > fix.patch

And now check returns no error message.

git apply fix.patch --check

Changing my .gitconfig file from

[color]
    ui = always

change to always

[color]
    ui = auto

Fixed my problem so I do not have to pass color option when diffing to patch file.

UPDATE: Based on saurabheights answer, you don't even need to brew link gnu-sed, you can do this with pearl. This will removed color characters from the bad patch file as well. There are probably many ways to do this.

perl -pe 's/\x1b.*?[mGKH]//g' bad.patch > good.patch

We tried debugging this for a few hours. What finally worked was this:

  1. Opened patch file with an editor like VS Code
  2. Changed encoding to UTF-8
  3. Changed line endings from CRLF to LF
  4. Saved the new file
  5. git apply myPatch.patch worked