25

I have never used patches with Git before and I need some help. I am trying to apply a patch to a Git repo to test a Wine patch, specifically this patch here. So I did the following:

$ git clone git://source.winehq.org/git/wine.git
$ cd wine
$ nano patch.p1

I then pasted the content of the patch with Ctrl+Shift+Vand used Ctrl+O to save. Then I tried this:

$ git am patch.p1
Patch format detection failed.

What am I doing wrong? I have never applied a patch before.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Aaron Franke
  • 3,268
  • 4
  • 31
  • 51
  • 7
    `git am` applies patches that are mailbox-formatted. The one you linked to is not. You could use `git apply` on it. However, the code in the patch is quite nonsensical unless there's a reversed condition (shifting by negative counts produces undefined behavior). – torek Mar 16 '18 at 04:50
  • 1
    @torek While [I am having trouble compiling Wine](https://unix.stackexchange.com/questions/430552/how-do-i-compile-wine-on-a-64-bit-ubuntu-system), `git apply` seems to have worked, as in it gives no errors. If you would like to post that comment to the patch creator: https://bugs.winehq.org/show_bug.cgi?id=44742 – Aaron Franke Mar 16 '18 at 05:21
  • 1
    I don't have a login there, nor intend to create one unless I actually start using Wine. But: `+ if(MaskLength <= 32) + return ERROR_INVALID_PARAMETER; + *Mask = 0xffffffff << ( 32 - MaskLength );` seems like the test should be `>=` not `<=`. 32-33 = -1 (or if unsigned, 0xffffffff), which is not a valid `<<` value. But I don't know what the intent of the function is. – torek Mar 16 '18 at 14:35
  • 1
    my problem was, I had generated the patch in windows, and tried to apply in linux and it wouldn't work. I had to apply it in windows. I believe it had to do with the CRLF settings between the two OS/Git configs – Vijay Nov 25 '20 at 15:50

2 Answers2

38

Patch format detection failed. probably means you're using the wrong command: use git apply instead of git am or the other way around.

See What is the difference between git am and git apply? for more on the difference between the 2.

user276648
  • 6,018
  • 6
  • 60
  • 86
6

Use below command: patch -p1 < patch_file_name.patch

  • You will be asked to specify "File to patch", mention complete path /
  • Assume -R [n]: n
  • Apply anyway? [n]: y

Do for all files present in you patch.

  • If any merge conflict occurs then check the conflict in ".rej" file which has been generated and resolve & apply those changes.
  • do "git add " and "commit" your changes.
Amit Kushwaha
  • 61
  • 1
  • 1