27
 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?

2
  • 1
    Can you share the contents of changes2.patch? Commented Jul 26, 2018 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. Commented Jul 26, 2018 at 19:52

7 Answers 7

30

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Just the --no-color option fixed it for me. The escape sequences to generate coloured output are obviously unwanted.
@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
This worked for me when creating the patch on a windows OS and applying it on a WSL ubuntu image
24

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.

2 Comments

Apparently, Powershell produces output in UTF-16, which get can not consume.
Here is a link with information on how to mitigate this in Powershell: stackoverflow.com/questions/40098771/…. Simple fix is to run $PSDefaultParameterValues['*:Encoding'] = 'utf8' first.
10

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

1 Comment

you da man! you pointed me in the right direction to fix a bug in lerna github.com/lerna/lerna/pull/2037
8

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

1 Comment

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.
2

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.

1 Comment

Thanks for your answer, I just run into the same case like you with git in PowerShell.
0

Just like @irsis, I also ran into this problem when I was generating the patch in PowerShell on Windows and then trying to apply that patch on Linux. Thanks to irisis, I find out the way to apply it. You don't need to install git shell on Windows, but with cmd.exe to generate the patch again. That patch can be applied on Linux. You can find that the patch file can be only half the size of that one generated in PowerShell.

Put in other words, use git command in CMD instead of PowerShell to generate patch file, while better to use --no-color option in case other issue.

Comments

0

Go the same problem after generating the patch from PowerShell. The classic 'dos2unix' command solved the issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.