How to apply a patch/diff and solve “hunk FAILED”, “can’t find file to patch” and others

by Markus Bertheau, last updated on January 28th, 2010 Leave a reply »

Hi :)

After you read this article, you’ll be able to apply patch files/diffs and deal with possible errors in the process.

Let’s assume the patch is in the file fix-bug.patch (the extension doesn’t matter). The directory software-1.0 contains the files to patch. Applying a patch file/diff then works like this:

  1. Make a backup. On Linux you can use the command:
    markus@ubuntu:~$ cp -a software-1.0 software-1.0.before-patch
  2. Run patch (for Windows: patch.exe) like this:
    markus@ubuntu:~$ patch -Np1 -d software-1.0 < fix-bug.patch

Attention: this is patch minus N p digit one, not minus N p small letter L!

If the result looks something like this:

markus@ubuntu:~$ patch -Np1 -d software-1.0 < fix-bug.patch
patching file bar.h
patching file quux.c
patching file foo.txt
markus@ubuntu:~$

then it worked and the patch file applied cleanly.

If you got different messages, they may be just warnings or they may be error messages. In any case the patch may have been applied incorrectly or only partially. To be safe, let’s restore from the backup first. Then we’ll figure out what the warning or error messages mean. To restore for Linux:

markus@ubuntu:~$ rm -rf software-1.0
markus@ubuntu:~$ cp -a software-1.0.before-patch software-1.0
Error messages and how to handle them

And now, here’s a list of messages that may appear. Each message links to a paragraph about what to do when this message appears.

1 comment

  1. nidhi says:

    it helped me greatly thanks a lot , very well informative article

Leave a Reply

© 2010 Markus Bertheau. All rights reserved.