16
 git diff 4ee42367 8c650199 > changes2.patch
 git checkout newBranch
 git apply changes2.patch
 error: unrecognized input

When i tried to apply the changes i'll get the error. What i'm doing wrong?

Nik Gaponov
  • 161
  • 1
  • 1
  • 4
  • 1
    Can you share the contents of `changes2.patch`? – Mureinik Jul 26 '18 at 15:36
  • I can't reproduce this behavior, but off hand I would guess that using the `--full-index` and `--binary` options for `git diff` might help. I'm assuming those hashes are either commit ID's, or appropriate tree ID's; and my guess is that you just have content that messes up a basic diff (as far as being able to create an applicable patch); but none of the combinations I tried got this exact symptom (at least, on my version of git), so I'm not sure. – Mark Adelsberger Jul 26 '18 at 19:52

4 Answers4

15

In case anyone else has this issue: for me, Powershell was the culprit. using Anentropic's answer from within git bash resulted in a "good" (readable) patch.

MCO
  • 1,037
  • 1
  • 10
  • 18
  • 3
    Apparently, Powershell produces output in UTF-16, which get can not consume. – Petr Oct 23 '20 at 15:07
  • 1
    Here is a link with information on how to mitigate this in Powershell: https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8. Simple fix is to run `$PSDefaultParameterValues['*:Encoding'] = 'utf8'` first. – Nate Apr 12 '21 at 20:25
13

I figured out this problem as instructed below,

  1. git checkout -b new_branch and commit something
  2. git diff master --no-color > your_patch_file.patch
  3. Open "your_patch_file.patch" file with NotePad++ and edit that file like,
    • Encoding > Convert to UTF-8
    • Edit > EOL Conversion > Unix (LF)
    • Save file
  4. git checkout master
  5. git apply your_patch_file_name.patch

Or you can run your git commands on Git Bash, probably you won't encounter any problem.

7

Do you have coloured output enabled by default?

If git diff 4ee42367 8c650199 shows up coloured in your terminal then the colour codes will get output to the patch file

Then git apply will fail with error: unrecognized input

In this case try instead with git diff --no-color

Anentropic
  • 28,868
  • 10
  • 92
  • 135
  • 1
    you da man! you pointed me in the right direction to fix a bug in `lerna` https://github.com/lerna/lerna/pull/2037 – Clay Apr 22 '19 at 22:37
6

For those running this form within Powershell, here is another post with information about the encoding error, and why it is happening.

If you're just looking for an answer, you can edit your powershell profile:

PS> code $profile

and add

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

This can cause other issues, so use at your own risk.

If you just want to run it for a single instance, you can do this

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
git diff > change.patch
git checkout otherbranch
git apply change.patch
Nate
  • 29,508
  • 21
  • 110
  • 179