51

I'm trying to apply a patch that includes binary files with git apply but only the files are added. I tried running git apply failing.patch -v and it prints something like:

Skipped patch 'file.txt'.
Checking patch file.bin...
Applied patch file.bin cleanly.

How can I find out what's the reason of the skip? As the current message is not very enlightening.

2
  • A patch is usually skipped when the changes it contains were already applied in the past. There are many possible reasons for this: a merge, a cherry-pick, changes operated manually or using another patch etc.
    – axiac
    Commented Feb 22, 2017 at 8:44
  • 4
    I thought about that too but that wasn't the case. Commented Feb 22, 2017 at 8:46

9 Answers 9

59

I found out the problem by running patch -p1 < failing.patch which printed:

can't find file to patch at input line 5

and reminded me that I was not in the root directory.

I can't understand why no one had asked this before and why is the verbose message not verbose.

Also, not even the official documentation mentions skipping and possible causes.

3
  • 2
    I had the same issue. Although this reference is Drupal specific, the reason can apply anywhere—that is, if any errors are there, git will not apply the patch, while the patch command works in a more piecemeal fashion.
    – karolus
    Commented May 8, 2018 at 13:53
  • This. Surprisingly in this case git behaves differently depending on working directory. After I moved to the root of the repository, patch application was successful.
    – Xeverous
    Commented Nov 19, 2021 at 11:03
  • That is wild because I could swear that patches sometimes apply cleanly if I am CD into the directory. But in this case, using --directory got me where I needed to be.
    – Loopy
    Commented Aug 4, 2023 at 15:33
38

If you use the --directory option to "git apply":

--directory=<root>

that path is RELATIVE TO THE BASE DIRECTORY (the one containing ".git"), not relative to the current working directory. You cannot use an absolute path either.

This is completely undocumented and took me hours to discover.

2
  • 1
    At least an error should be provided to indicate the directory was not found, but all I see is a generic "skipped patch" error (and only if verbose logging is on).
    – ealfonso
    Commented Dec 3, 2020 at 22:04
  • 1
    Oh, wow, thanks. This was also my problem. I did not use the correct relative path. There really should be some better feedback from git about what is going on.
    – jcxz
    Commented Apr 27, 2021 at 8:45
9

Suffering this issue whilst attempting to port changes across projects. git apply seems to ignore any directory names on the patch file paths, also it refuses to apply if the Index line does not match a file hash in the target repository. I had better success using these options (of which --no-index seems to be undocumented):

git apply --verbose --no-index --directory {subdir} {patch-file} 
7

Got the same issue. In my case the source of error was .git folder in some of parent directories of the patch target. The solution was to move patch target outside that parent directory.

2
  • 1
    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. A different workaround is to call "git apply -v path/to/myPatch.patch" from the root directory, and ensure that the patch file itself has the path/to/fileToBePatched.file in it which leads to the file from the root directory.
    – fallerd
    Commented Dec 19, 2019 at 19:40
  • I was working with a generated project in an 'ignored' folder. Doing git init <MY_FOLDER> allowed the patches to be applied.
    – Peter L
    Commented Jun 1, 2021 at 20:03
2

I put together a solution based on other answers here but had to still do some research. This post is similar to others but fills in the pieces I was missing from the others. Hopefully, it will save someone some time.

In my case, there was an additional directory level in the target repo compared with the source repo. In other words, the top-level folder in the source repo, source-top-level, was contained by a parent, target-top-level, in the target repo. However, since there were no errors, I couldn't be sure that was the issue. So I used --verbose, which shows the errors and I could confirm that indeed it was the issue.

I then checked the git documentation for the --directory option. It allows you to specify a string that will be prepended to the path of each of the files in the patch:

--directory=<root>
 Prepend <root> to all filenames.

By specifying --directory=target-top-level it transformed each path, source-top-level/some-path, to target-top-level/source-top-level/some-path and with that the git apply was successful.

0

use the regular patch command.

I was running into this as (probably I shouldn't) I have two trees inside my one repo, and I was trying to apply a diff onto another tree within the same repo. Using patch instead of git apply did what I wanted :-)

0

When doing a git apply, I discovered that if there is any error on applying the patch to just one file, then none of the files are patched on the apply.

When I removed the file causing the git apply error from the patch file, and then reapplied the patch, all of the remaining files were patched.

1
0

There's something strange in git apply implementation on Windows (git 2.41.0.windows.1, Windows 11, PowerShell 7.x).

(Context: Trying to apply a set of patches generated with git format-patch)

Applying a patch with patch -pX -i patchfile works without issues, but git apply -pX patchfile always skips the the patch -- doesn't seem to make any difference what additional options are used. No error messages whatsoever. GIT_TRACE=1 and GIT_DEBUG=1 reveal absolutely nothing.

However, if I apply the same patch (or set of patches) using cat $(gci *.patch) | git am -pX (In PowerShell) the patches are applied successfully.

0

can't find file to patch at input line 5

I dont know the content of your patch file

But I think you should correct the file path inside the patch file, eg:

Change the path of files

diff --git a/web/modules/contrib/entity_reference_revisions/entity_reference_revisions.module b/web/modules/contrib/entity_reference_revisions/entity_reference_revisions.module
index 82c3cd7..230f2b8 100644
--- a/web/modules/contrib/entity_reference_revisions/entity_reference_revisions.module
+++ b/web/modules/contrib/entity_reference_revisions/entity_reference_revisions.module

To

diff --git a/entity_reference_revisions.module b/entity_reference_revisions.module
index 82c3cd7..230f2b8 100644
--- a/entity_reference_revisions.module
+++ b/entity_reference_revisions.module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.