44

I'm trying to apply a patch, and git apply patch doesn't give me any errors:

sashoalm@SASHOALM-PC /c/Workspace/tesseract-git/api (master)
$ git apply ../../commit-d0d9477

sashoalm@SASHOALM-PC /c/Workspace/tesseract-git/api (master)
$

As you can see, no error messages are given. But when I open the history, nothing is committed, and the index is empty. It's as if I haven't issued the git apply command.

I'm at a loss how to troubleshoot this, since there no errors to Google.

Edit: I forgot to say, but I'm trying to transplant my patches from a repository of Tesseract, where I got the source without version control, created my own empty git repo, and made some commits. Later I cloned the Tesseract repository using git svn, and now I'm trying to move my patches there.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Maybe the changes, the patch tries to apply, are already in your repository? In that case the patch would apply but nothing would change. At least that's what I think it does. – Sascha Wolf Jul 18 '14 at 11:14
  • @Zeeker Not in my case - those changes were made by me, there's no way they would be in the official repo I cloned. – sashoalm Jul 18 '14 at 13:31

2 Answers2

84

I found this while googling for the issue: http://data.agaric.com/git-apply-does-not-work-from-within-local-checkout-unrelated-git-repository

git apply will fail to do anything when used within a local checkout of a git repository (other than the one for the project the patch is made for), such as if you are patching a module that is within a site that is in Git version control.

Use patch -p1 < path/file.patch instead.

No Sssweat
  • 358
  • 1
  • 6
  • 24
B_.
  • 2,164
  • 2
  • 17
  • 20
  • 39
    I was pulling my hair out trying to understand why git returns success (status 0) but does not apply patches when I try to run it from a subdirectory. Now I know that I have to always make sure that `git apply` is run from the root of my repository. This might be tricky in automated scripts. – JustAMartin Dec 11 '15 at 10:37
  • 4
    Thanks. I was applying git patch from a subdirectory and it was failing silently. Just had to go to root and apply patch and it was happy. – Jignesh Nov 11 '17 at 19:25
  • 1
    Thanks, was equally irritated that i can't trust git apply now ! – Deepan Prabhu Babu Jan 23 '19 at 16:02
  • 1
    Thanks, @JustAMartin, you saved my bacon (and sanity). – Bogatyr Dec 06 '19 at 06:51
  • For me the solution was to call `git apply` in the root directory of the git repository (it was a diff from one commit to another of that same repo and I didn't call `git diff` in the repos root dir) – polynomial_donut Dec 15 '22 at 17:04
1

Further to B_'s answer, you might think that using git diff --no-prefix would mean you don't need to use the -p flag on patch. It appears you would be wrong, at least in the versin of patch I have here (2.5), no -p flag is not the same as -p0. -p0 works.

Pete
  • 1,241
  • 2
  • 9
  • 21