How to use diff and patch in your project

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


4 Replies to “How to use diff and patch in your project”

  1. 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 :)

  2. 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.

Comments are closed.