Error while applying a patch in git
Asked Answered
B

4

20

I have a shallow clone, on which i made three commits. Here is the log:

$ git log --oneline --graph --decorate --all
* d3456fd (HEAD, master) patch 3
* 9713822 patch 2
* 6f380a6 patch 1
* 8a1ce1e (origin/master, origin/HEAD) from full clone
* 7c13416 added from shallow
* 3b3ed39 removed email
* cfbed6c further modifications
* a71254b added for release 2.1
* 7347896 (grafted) changes for release 2

now i create a patch from here:

$ git format-patch -k --stdout origin > ../format_since_origin.patch

I want to apply this patch in another clone, which is a full clone.
Here is the log:

$ git log --oneline --graph --decorate --all
* 8a1ce1e (HEAD, origin/master, master) from full clone
* 7c13416 added from shallow
* 3b3ed39 removed email
* cfbed6c further modifications
* a71254b added for release 2.1
* 7347896 changes for release 2
* b1a8797 changes to ttwo files
* 603710c changed test report
* 16b20b3 added test_report.txt
* f0871ea modified file1.xml
* dd94bfc added file1.xml
* 00758aa second commit
* 49f9968 first commit

I am unable to apply the patch created from the shallow clone above. I get the following error.

$ git am -3 /c/temp/git/format_since_origin.patch
Applying: patch 1
Using index info to reconstruct a base tree...
error: patch failed: file1.c:6
error: file1.c: patch does not apply
Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Cannot fall back to three-way merge.
Patch failed at 0001 patch 1
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".

Any idea why this patch is failing? Or is my method totally wrong?

Update:

It works with the following

$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch Applying: patch 1 Applying: patch 2 Applying: patch 3

Now, as suggested by Charles - if i try the git diff, i get the error as below.

$ git diff -p origin > ../dif_origin.patch

On applying,

$ git apply --ignore-whitespace --inaccurate-eof /c/temp/git/dif_origin.patch
c:/temp/git/dif_origin.patch:9: trailing whitespace.
patch change for file1.c
c:/temp/git/dif_origin.patch:18: trailing whitespace.
patch this xml guy
c:/temp/git/dif_origin.patch:29: trailing whitespace.
fsdfsd
c:/temp/git/dif_origin.patch:30: trailing whitespace.
patch this report
error: patch failed: file1.c:6
error: file1.c: patch does not apply
error: patch failed: file1.xml:2
error: file1.xml: patch does not apply
error: patch failed: tr/test_report.txt:2
error: tr/test_report.txt: patch does not apply

Brougham answered 2/11, 2012 at 6:54 Comment(3)
Is there a reason not to use git bundle or even git diff/git apply?Tinkle
@charles, i get errors on using git diff and apply. i have added the error details in the original post. Did i create the diff wrong?Brougham
Can also occur if you get an obo with your SHAs in your patch. If you include a SHA that's already in the "to be patched" repo, you'll get this error since the original states don't match. Eg, git format-patch -k --stdout SHA_AlreadyAppliedToBothRepos..SHA_LastWagoner
S
18

Note that one rationale for having to ignore whitespace was (June 2010):

What it does is enable the GMail -> download -> git-am workflow.
GMail (and doubtless countless other) E-Mail providers introduce whitespace at the beginning of raw E-Mail messages, while otherwise leaving them intact.

As mentioned in "git am/format-patch: control format of line endings", you can try a:

 git am --keep-cr

That wouldn't require you to ignore whitespace (warning only).

The OP maxmelbin confirms in the comments that the following works:

 git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin.patch
Sticky answered 2/11, 2012 at 7:15 Comment(0)
K
20

When git apply is working normally, you get no output at all:

$ git apply example.patch
[nothing returned]

If you want to see what's going on behind the scenes, you can use the -v (verbose) flag:

$ git apply -v example.patch
Checking patch includes/common.inc...
Applied patch includes/common.inc cleanly.

However, if running git apply from within your own local git working copy, it's possible that even with the -v (verbose) flag, git apply will do nothing, and give no output. If this happens, please check your location in the directory tree - git apply may work from another location.

An alternative to git apply is to use the patch command:

$ patch -p1 < example.patch

Here is other output the git apply command can generate, and what it means. Patch does not apply

$ git apply example.patch
error: patch failed: includes/common.inc:626
error: includes/common.inc: patch does not apply``

Git couldn't apply the changes in the patch because it wasn't able to find the line(s) of code in question; they must have been changed or removed by another commit. Try these things:

Make sure the patch hasn't already been applied. Look for it in git-log or simply examine the code to see if the change(s) are already present. If they are, you're done. If they aren't or if only some of them are, try something else:

Use patch -p1 < filename.patch. Whereas git-apply altogether rejects a patch with any errors, patch -p1 works hunk by hunk, applying as many individual changes as it can. It backs up each file as filename.ext.orig before modifying it and saves rejected hunks in filename.ext.rej. Discard the .orig files and manually apply the changes left in the .rej. This is an easy strategy for small patches.

Killdeer answered 18/11, 2014 at 11:50 Comment(0)
S
18

Note that one rationale for having to ignore whitespace was (June 2010):

What it does is enable the GMail -> download -> git-am workflow.
GMail (and doubtless countless other) E-Mail providers introduce whitespace at the beginning of raw E-Mail messages, while otherwise leaving them intact.

As mentioned in "git am/format-patch: control format of line endings", you can try a:

 git am --keep-cr

That wouldn't require you to ignore whitespace (warning only).

The OP maxmelbin confirms in the comments that the following works:

 git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin.patch
Sticky answered 2/11, 2012 at 7:15 Comment(0)
B
10

ok. the following worked .

$ git am -3 --ignore-whitespace /c/temp/git/format_since_origin.patch
Applying: patch 1
Applying: patch 2
Applying: patch 3

Brougham answered 2/11, 2012 at 7:1 Comment(1)
as per VonC reply, the following will also generate the same SHA1 - git am -3 --keep-cr --committer-date-is-author-date /c/temp/git/format_since_origin.patchBrougham
A
0

If you're using intellij idea, just go to toolbar->git->patch->create/apply patch, this is the simplest idea and it won't throw any error while applying the patch.

Augustinaaugustine answered 3/12, 2022 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.