How to use diff and patch in your project

advertisement logo

 

Diff and patch are two separate tool that are often use together, particularly in software development. The use of these tools can ease up the process of recording differences and applying changes between two files.

diff is a tool that can be use to create a “diff” or “patch” file that contains differences between two files. Though diff can write into many different format, most people will prefer the unified format as it is easier to work with.

patch is another tool that complements diff, it will apply the differences in the “patch” file to the target file. Think of it is a way to “patch” your old files with newer modifications.

The guide below is the simplest way to use diff and patch. Though the information provided here is incomplete, I believe it can get you started in using them in your project.

How to use diff

The basic use of diff is,
diff -u original.txt modified.txt > file.patch

If you want to use diff against two source tree, the command is,
diff -rupN original modified > program.patch

How to use patch

To apply the patch, change into the same directory as the unmodified file and execute
patch < file.patch

This is how to apply patch to an entire directory,
patch -p0 < program.patch

Patch applied can be simply removed by adding the -R switch,
patch -p0 -R < program.patch
patch -R < file.patch

For the explanation of using the -p parameter, please read Applying patch to other directories

Other reference : LinuxJournal

unix,guide,tips,diff,patch,tutorial


Share this post

Keep updated with the latest posts, be a part of over 1,000 subscribers! : email iconSubscribe to your email

4 smashing comments for this post.

  1. k4ml Said:

    you might also want to try kde kompare. that’s the tools that I used for a huge merge operation, i.e merging two different branch of your project. since it allows you two visually look at the difference and selecting which diff to apply it gives high degree of confidence that you’ll not break your tree. the only problem is (which exist since the earlier version till the one that I used – 3.4) new file will be created as directory unless you created an empty one prior to running kompare. OTOH, vimdiff also would be usefull if you only need to compare between two files.

  2. kaeru Said:

    I wrote an article with examples a while ago:

    http://www.iosn.net/Members/kaeru/articles/software-development/patches/

    I prefer Meld myself for visual diffs.

  3. k4ml Said:

    meld: cools !

    but it was a gnome apps. I had a really bad experience trying to get gnome up on my freebsd 5.4 last week to the extent that I swear I’ll never touch gnome anymore after this. I’m still not recovered yet but sure I would give meld a try once the trauma fade away. thanks for pointing out :)

  4. saif Said:

    i want to know how can use the patch

Leave a Comment

Subscribe by email

Enter your Email