Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while applying a patch in git

Tags:

git

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

like image 454
maxmelbin Avatar asked Nov 02 '12 06:11

maxmelbin


People also ask

How do I fix failed patch?

To fix the Failed Patch error it is nice and simple to sort. Just make sure to close the game first and make sure that there is an update installing for the title. If there is, let it finish installing and then boot up the game. Afterward, this error will be fixed for you.

Why is git apply skipping patch?

To add to this - patches inside of git repositories can only be applied from the repo's root directory, so doing "git apply -v myPatch. patch" inside a subdirectory of a git repository will skip that patch even if this command works when the files are placed in any other directory not contained by a repo.

What is Apply patch in git?

GIT patch or GIT diff is used to share the changes made by you to others without pushing it to main branch of the repository. This way other people can check your changes from the GIT patch file you made and suggest the necessary corrections.

How do I apply a patch file in Visual Studio code?

Right-click the item in the Solution Explorer and select Subversion > Apply Patch. Open the patch file. TortoiseMerge runs to merge the changes from the patch file with your working copy.


2 Answers

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
like image 115
VonC Avatar answered Sep 30 '22 14:09

VonC


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.

like image 33
Manish Shrivastava Avatar answered Sep 30 '22 12:09

Manish Shrivastava