25
 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
  • 251
  • 1
  • 3
  • 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

5 Answers5

29

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.

  • 1
    Just the `--no-color` option fixed it for me. The escape sequences to generate coloured output are obviously unwanted. – NeilG Jun 19 '22 at 11:08
  • @NeilG I made the patch with `--no-color` flag, still it gave me the same error, I think this should be the accepted answer. This solved the issue form me. My OS is Win11 – runtimeTerror May 19 '23 at 07:32
22

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,187
  • 1
  • 11
  • 20
  • 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
8

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
  • 32,188
  • 12
  • 99
  • 147
  • 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
7

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
  • 30,286
  • 23
  • 113
  • 184
  • 1
    Thanks, this answer is actually almost exactly what I needed. GIT was still unappreciative of my the CLRF lines (which I recognize can largely boil down to git settings as well). I fixed that (with cygwin vi), and things were good. – wrestang Jan 31 '23 at 20:11
1

In my case problem was that I was generating the patch in PowerShell in Windows and then trying to apply that patch in Linux.

After looking frantatically on web for hours I found problem was due to PowerShell. I then created the patch using git bash in Windows and applied that on the Linux without any issue.

Looks like there might charset differernce or powershell might adding some characters which git did not understant on Linux.

Posted my answer here as it might help people who land up on this question with this error.

irsis
  • 952
  • 1
  • 13
  • 33