All GIT patches I create throw fatal: unrecognized

2019-06-16 19:52发布

问题:

I'm running Mac OSX 10.9.4 (Mavericks) and have git version 2.8.2. I've tested this with a completely new repo. Here's example.

mkdir gitest
cd gitest
git init
echo "monkeyface" > monkey.txt
git commit -m "first commit"
echo "monkeyface farted" > monkeyfart.txt
git add .
git diff HEAD > new.patch
rm monkeyfart.txt
git reset --hard HEAD
git apply new.patch --check
>fatal: unrecognized input

Any ideas what is causing this? Could it be anything in my .gitconfig file?

[user]
    name = myusername
    email = myemail@mail.com
[color]
  ui = always
[alias]
  st = status -sb -uall
  lg = log --decorate --pretty=oneline --abbrev-commit --graph
  undocommit = reset --soft HEAD^
  undopush = push -f origin HEAD^:master
[core]
    editor = vim
    excludesfile = ~/.gitignore_global
    pager = less -r
[commit]
  template = ~/.gitmessage.txt
[filter "media"]
    clean = git-media-clean %f
    smudge = git-media-smudge %f

UPDATE:

While the answer linked below offers some idea on what the problem might have been, my issue was specifically hidden in my configuration since no color argument was being passed into the command. This answer is relevant but my question and answer might be helpful to others who may experience a similar issue.

Extract changes from diff file to current branch

回答1:

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
    


回答2:

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


回答3:

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


回答4:

For those who already have a patch with color codes, try this:-

On Ubuntu:-

cat incorrect.patch | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > correct.patch

On Mac, install gsed using:-

brew link gnu-sed

and to generate correct patch file:-

cat incorrect.patch | gsed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > correct.patch