diff and patch are a complimentary pair of commands for viewing and applying changes to a file. Let’s assume that we have a simple C program (hello.c):
I’m now going to make some modifications to it in a NEW file (hello_extended.c):
int main(int argc, char **argv)
{
if (argc == 2)
{
printf(”Hi! %s\n”, argv[1]);
}
printf(”Hello World\n”);
return 0;
}
My next move is to take a ‘diff’ file of the two which is an instruction set of changes:
-int main(void)
+int main(int argc, char **argv)
{
+
+ if (argc == 2)
+ {
+ printf(”Hi! %s\n”, argv[1]);
+ }
printf(”Hello World\n”);
return 0;
}
Now I can pass my hello.diff file to the developers of hello.c and they can perform:
hello.c is now a patched version containing my changes.
int main(int argc, char **argv)
{
if (argc == 2)
{
printf(”Hi! %s\n”, argv[1]);
}
printf(”Hello World\n”);
return 0;
}
Why dont I just pass along my hello_extended.c rather than going to this trouble?
Well, if you look at hello.diff it contains date information, line numbers and positions which will help in the event of conflicts between other changes, as well as an exact map on how to reproduce the changes, rather than just the changes themselves, which is a lot more welcome on a larger file.