Monday, March 30, 2015

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.

No comments :

Post a Comment