Patch me gently

Have you ever asked yourself what a patch exactly is? Well – let’s take a closer look at the things your mother used to fix your trousers – a patch. That’s quite close as it fixes a hole. But patches in the it field can be even more than just a fix. If you want to see a really huge patch that’s improving the kernel, you might want to look at the latest kernel-snapshot – it’s more or less a howto on changing the code the way the developer use for saving some bandwidth as they don’t need to transfer the unchanged files.

But that’s the theory. Let’s get down to the shell and let’s have a look at a small utility named ‘diff’. Diff, as the name might suspect shows differences between files, so you can see the changes easily. But if you compare it to the patch from above, they look quite different. But let’s have a look at it with some parameters:

diff -Naur file.old > update.patch

Et voilà – the file we got now looks like a patch should look like. Diff can compare files or even directories and make a patch. If you are a developer it even works with revision control.

Well… enough about patch generation. We got our patch now. But how to work with it? Let’s have a look at the other side – we got the old file and our patch.

As we’re in the same directory as the patch is, we can safely use the -p1 parameter for skipping the path stuff. But if we’re using an absolute path, -p0 is the way to go:

patch -p0 < update.patch patch -p1 < update.patch

The following output should look that way:

patching file file.old

If everything worked, and file.old should be the same now, as our patch updated file.old to the new version.


One thought on “Patch me gently”

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.