Mastering Diff and patch

Diff and patch are essential if you are working on a large project with multiple contributors. They are a boon for programmers distributed across the globe and wanting to work on a single project.

These tools are present by default on a linux or Unix based operating system.

Diffing individual file

$diff -u originalFile modifiedFile > output.patch

This creates a file `output.patch` containing the diff of the two files, i.e. the original file and the modified file.

Diffing entire directories

$ diff -ur originalDirectory modifiedDirectory > output.patch

This creates a patch file containing all the modifications

Applying a patch to an individual file

When you say you are applying a patch to a file, you are basically applying a diff to an original file.

$ patch -p < patchFile

The diff file and the original file should be in the same directory.

Patching entire directories

cd to the parent directory of the directory to which patch has to be applied.

$patch -p1 < patchFile

The number 1 basically says that the number of directories to be ignored while applying the patch is 1.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s