540

I have a certain patch called my_pcc_branch.patch.

When I try to apply it, I get following message:

$ git apply --check my_pcc_branch.patch
warning: src/main/java/.../AbstractedPanel.java has type 100644, expected 100755
error: patch failed: src/main/java/.../AbstractedPanel.java:13
error: src/main/java/.../AbstractedPanel.java: patch does not apply

What does it mean?

How can I fix this problem?

4
  • Are there any AbstractedPanel.java.rej files lying around? Typical this means that a line bot changed in the source as well as in the patch (here line 13 seems to be affected).
    – Rudi
    Commented Jan 24, 2011 at 10:44
  • No, I didn't find any *.rej files. Commented Jan 25, 2011 at 12:56
  • Have you tried reporting a bug to [email protected]? None of the described options of Git apply work for me returning tons of errors. I had to fall back to WinMerge. Commented Sep 24, 2020 at 19:21
  • For me it meant I was git apply'ing a patch that was already in there FWIW...shame it doesn't give a more meaningful message...
    – rogerdpack
    Commented Apr 5, 2023 at 20:55

15 Answers 15

718

git apply --reject --whitespace=fix mychanges.patch worked for me.

Explanation

The --reject option will instruct git to not fail if it cannot determine how to apply a patch, but instead to apply the individual hunks it can apply and create reject files (.rej) for hunks it cannot apply. Wiggle can "apply [these] rejected patches and perform word-wise diffs".

Additionally, --whitespace=fix will warn about whitespace errors and try to fix them, rather than refusing to apply an otherwise applicable hunk.

Both options together make the application of a patch more robust against failure, but they require additional attention with respect to the result.

For the whole documentation, see https://git-scm.com/docs/git-apply.

9
  • 11
    This actually worked better for me because it didn't completely modify my file Commented Jan 7, 2014 at 21:05
  • 14
    This is great. Just rejects what it cannot solve itself and you can then just modify the rejected files manually.
    – Dennis
    Commented Jul 3, 2014 at 14:27
  • 11
    this command creates .rej files when can't automatically detect how to apply a patch. You could use wiggle to resolve such issues. Commented Apr 30, 2016 at 11:21
  • 20
    This answer does not explain anything, in particular in which cases it will work. People, you really have to be more demanding of answer quality, this is SO not a forum.
    – Oliver
    Commented Jan 25, 2019 at 15:24
  • 2
    This, followed by wiggle --replace file file.rej and solving remaining conflicts in file (help by beautiful IDE support) fixed everything. Thanks a lot, made my day :pray:
    – silopolis
    Commented Jun 3, 2022 at 10:02
413

Johannes Sixt from the [email protected] mailing list suggested using following command line arguments:

git apply --ignore-space-change --ignore-whitespace mychanges.patch

This solved my problem.

10
  • 31
    Can anyone help me and explain why this works? The other answer did not work for me, and I had the exact same problem as what the question asker describes. What do file attributes have to do with ignoring whitespace?
    – skrebbel
    Commented Apr 13, 2011 at 11:43
  • 1
    Using windows powershell A patch made with git diff was successfully applied as follows: git diff HEAD..613fee -- myfile.xml | git apply --ignore-space-change --ignore-whitespace, whereas first saving the diff output as a file did not work, in case anyone runs into the same problem
    – tjb
    Commented May 11, 2012 at 10:50
  • 3
    also try -C1 switch for apply, it reduces the context around additions that are regarded as important. Commented Dec 23, 2012 at 13:05
  • 3
    @EricWalker, git magic with CR/LF isn't necessarily a bad thing. The alternative can be that half of your changesets consists of every single line in every file that was touched being changed from one line ending to the other, with the actual change buried somewhere in the middle.
    – jwg
    Commented Mar 21, 2013 at 9:30
  • 3
    This helps sometimes. But other times, I still get the "patch does not apply", even though the patch should apply without issues. Commented Apr 18, 2016 at 9:56
282
+50

When all else fails, try git apply's --3way option.

git apply --3way patchFile.patch

--3way
When the patch does not apply cleanly, fall back on 3-way merge if the patch records the identity of blobs it is supposed to apply to, and we have those blobs available locally, possibly leaving the conflict markers in the files in the working tree for the user to resolve. This option implies the --index option, and is incompatible with the --reject and the --cached options.

Typical fail case applies as much of the patch as it can, and leaves you with conflicts to work out in git however you normally do so. Probably one step easier than the reject alternative.

11
  • 4
    This is the answer that worked for me. The file I was patching didn't reflect the changes that I generated the patch from (because I deleted the changes after I created the patch.)
    – Christia
    Commented May 16, 2018 at 5:02
  • 3
    Nice general solution. The 3way diff didn't look like it normally does so a little confused by that but never the less this gave me the ability to resolve the conflict and get the patch to apply.
    – steinybot
    Commented Jul 12, 2018 at 4:17
  • 28
    I think this --3way should be the default behavior. When patching fails, at least tell me what failed so that I can manually fix it. git apply just fails and doesn't report why something fails. I couldn't even find *.rej files like the ones hg generates. Commented Aug 3, 2018 at 1:48
  • 14
    Definitely, the best solution. Let the user resolve his own conflicts!
    – Mosh Feu
    Commented Dec 18, 2018 at 15:51
  • 3
    Doesn't work for me in the case where the issues are caused by whitespace differences. I never get a prompt, and --3way doesn't actually apply anything. error: patch failed: Foo.cs:4 Falling back to three-way merge... error: patch failed: Foo.cs:4 error: Foo.cs: patch does not applyHowever, on the same patch file, it will apply if I ignore whitespace (then I have to go fix up the whitespace).
    – Nathan
    Commented Sep 10, 2020 at 16:36
67

This command will apply the patch not resolving it leaving bad files as *.rej:

git apply --reject --whitespace=fix mypath.patch

You just have to resolve them. Once resolved run:

git -am resolved
3
  • 10
    how to resolve *.rej - all I can find is to make the changes manually in the source file & delete these .rej files. Any other way ? Commented Apr 7, 2015 at 5:18
  • 1
    @coding_idiot As usual, Just check the .rej files, compare them with the conflicting files and finally add the fixed files to the index (with "git add FIXED_FILES") Commented Apr 23, 2015 at 11:19
  • 5
    @coding_idiot you could use wiggle to resolve it. For example: wiggle --replace path/to/file path/to/file.rej. This command will apply changes from .rej file to original file. Also it creates a copy of original file, like path/to/file.porig. Please, checkout documentation to get more info about wiggle Commented Apr 30, 2016 at 11:19
48

Try using the solution suggested here: https://www.drupal.org/node/1129120

patch -p1 < example.patch

This helped me .

7
  • 5
    I know you're not supposed to do this, but THANK YOU SO MUCH! Saved me hours. I was getting "patch does not apply" and all sorts of errors. Commented Nov 9, 2018 at 22:25
  • 1
    @sudorm-rfslash, why are we not supposed to do this and why were you doing it nevertheless?
    – Black
    Commented Feb 24, 2020 at 8:15
  • git: 'patch' is not a git command. on git version 2.21.1 (Apple Git-122.3) Commented Mar 9, 2020 at 18:10
  • 3
    @SridharSarnobat The command is patch, not git patch.See patch(1)
    – HeySora
    Commented Nov 17, 2020 at 12:54
  • 2
    This is the way. Forget git patch Commented Oct 11, 2022 at 4:21
18

It happens when you mix UNIX and Windows git clients because Windows doesn't really have the concept of the "x" bit so your checkout of a rw-r--r-- (0644) file under Windows is "promoted" by the msys POSIX layer to be rwx-r-xr-x (0755). git considers that mode difference to be basically the same as a textual difference in the file, so your patch does not directly apply. I think your only good option here is to set core.filemode to false (using git-config).

Here's a msysgit issue with some related info: http://code.google.com/p/msysgit/issues/detail?id=164 (rerouted to archive.org's 3 Dec 2013 copy)

3
  • 2
    I tried to run the command "git config core.filemode false", but it didn't help - I'm still getting the same message. Commented Jan 22, 2011 at 20:19
  • 1
    Assuming you have no un-committed changes in your tree, try git reset --hard HEAD to force git to re-checkout your files with the new option in effect. Commented Jan 22, 2011 at 20:25
  • Just tried it out execute "git reset --hard HEAD". It was successful (I saw the message "HEAD is now at ..."), but the problem with "git apply" persists. Commented Jan 22, 2011 at 22:46
18

Just use git apply -v example.patch to know the reasons of "patch does not apply". And then you can fix them one by one.

9

git apply --reverse --reject example.patch

When you created a patch file with the branch names reversed:

ie. git diff feature_branch..master instead of git diff master..feature_branch

8

In my case I was stupid enough to create the patch file incorrectly in the first place, actually diff-ing the wrong way. I ended up with the exact same error messages.

If you're on master and do git diff branch-name > branch-name.patch, this tries to remove all additions you want to happen and vice versa (which was impossible for git to accomplish since, obviously, never done additions cannot be removed).

So make sure you checkout to your branch and execute git diff master > branch-name.patch

1
1

If the patch is only partly applied, but not the entire patch. Make sure you are in the correct directory when applying the patch.

For example I created a patch file in the parent project folder containing the .git file. However I was trying to apply the patch at a lower level. It was only applying changes at that level of the project.

3
  • This put me in the right direction. I was in the incorrect directory when applying the patch, because of which the patch was not applying. Thanks Commented Jun 15, 2022 at 9:29
  • @MShubat what do you mean by this - I created a patch file in the parent project folder containing Commented Oct 31, 2023 at 19:43
  • @DhruvSaraswat how do you basically decide what is the correct directory to apply the patch ? Commented Oct 31, 2023 at 19:44
1

Make sure you are:

  1. Running the git apply command from the root of the repository (weird, but that was the only correct solution for me)
  2. You are running git apply without --check option (in that case only the check is executed, but the patch itself is not applied)

Hope that helps!

1
  • First one helped me!! Commented Jul 12 at 16:42
0

My issue is that I ran git diff, then ran git reset --hard HEAD, then realized I wanted to undo, so I tried copying the output from git diff into a file and using git apply, but I got an error that "patch does not apply". After switching to patch and trying to use it, I realized that a chunk of the diff was repeated for some reason, and after removing the duplicate, patch (and presumably also git apply) worked.

1
  • @VirendraKumar I manually edited the diff file to remove the duplicate lines. I then used patch. (git apply would probably also work.) Commented Jul 15, 2020 at 21:05
0

In my case, I was trying to apply changes from another branch b on main (not from a patch file). I received the same error and the only alternative automatic solution was $ git checkout <branch_name> -- .

This command will update the files in your working directory to match the ones in <branch_name>, but it won’t change your HEAD pointer. This means you’ll stay on your current branch, but your files will match those in <branch_name>.

0

In one case I got "patch does not apply" because when I created the patch with

git diff hash1..hash2 > mypatch.diff

I exchanged the position of hash1 and hash2, so I got a "revert my changes" patch.

-1

What I looked for is not exactly pointed out in here in SO, I'm writing for the benefit of others who might search for similar. I faced an issue with one file (present in old repo) getting removed in the repo. And when I apply the patch, it fails as it couldn't find the file to be applied. (so my case is git patch fails for file got removed) '#git apply --reject' definitely gave a view but didn't quite get me to the fix. I couldn't use wiggle as it is not available for us in our build servers. In my case, I got through this problem by removing the entry of the 'file which got removed in the repo' from patch file I've tried applying, so I got all other changes applied without an issue (using 3 way merge, avoiding white space errors), And then manually merging content of file removed into where its moved.

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.