How do I apply a patch?

The answer to this depends on how the patch was created, specifically to which directory the patch is done from. In general though, patches are done in the root of the source tree (/usr/src/linux), and the following assumes this.

For example's sake, you have unpacked a tarball of Linux 2.4.0, and you want to apply Linus' patch-2.4.1.bz2, which you have placed in /usr/src. Do the following :

        cd /usr/src/linux
        bzip2 -dc /usr/src/patch-2.4.1.bz2 | patch -p1 --dry-run

We used the --dry-run option to check that the patch applies cleanly. This can be a life-saver sometimes as it can be a real pain to back out a partially-applied patch. The -p1 option strips off part of the diff file's pathnames for each changed file (see the patch(1) manpage for more details). Now you've checked that it should apply cleanly, run the following to actually apply it. Then you're done!

        bzip2 -dc /usr/src/patch-2.4.1.bz2 | patch -p1

This is actually simple with Linus' standard patches, as you can use the script linux/scripts/patch-kernel to automatically do the patches for you.

The situation with other patches is not always so simple. For example, Linus' pre patches (found in pub/linux/kernel/testing) are not incremental, That is pre10.bz2 must be applied on top of the tarball of the previous full release kernel. E.g., patch-2.4.8-pre2 goes on top of an unpacked 2.4.7 tarball, *not* on top of a patched 2.4.8-pre1 kernel. If you have a 2.4.8-pre1 kernel, you can get back to 2.4.7 by following the section 'Reversing a patch' below.

Alan Cox's ac patches ( and Andrew Morton's mm patches ( usually follow the same method.

Occasionally you may want to test a patch from linux-kernel or similar. Generally these will be incremental against the named version (so, say, 2.4.0-test1-ac22-hosedmm.diff should be applied against 2.4.0-test1-ac22), and relative to the root. You may need to play with the -p option.

Reversing a patch

You've applied several patches, and now you want to remove them. Simply use the -R option to patch, with the same patch file, to back out the patch (alas, the patch(1) manpage is less than clear on this).

Patching using git

if you have the patch, try this.

        git-apply --whitespace=error-all <patchfile>

See the man page of git-apply for more info.

Points to note while applying patch from e-mails.

A majority of the patches are circulated through e-mail body (as opposed to attachments). Some mail clients (notably gmail web interface) would not show <TAB> character as a single char. Instead you would end up TABs replaced by spaces. Sometimes, you might get wrong patch or broken patch messages from git/patch. You can avoid such problems by using the raw data view (generally supported in all web based mails including yahoo! and gmail) to copy the patches from. Raw view does not render tabs as spaces and space only lines as empty lines.


KernelNewbies: FAQ/HowToApplyAPatch (last edited 2021-01-10 22:55:55 by RandyDunlap)