Why use diff/patch when it is easier to just use cp

Diffs can be more complicated than just comparing one file versus another. The can compare entire directory hierarchies. Consider the example that I want fix a bug in GCC. My change adds a line or two in 4 or 5 files and deletes a handful of lines in those and other files. If I want to communicate these changes to someone, potentially for inclusion into GCC my options are

  • Copy the entire source tree
  • Copy only the files that were changed
  • Supply just the changes I've made

Copying the entire source tree doesn't make sense, but what about the other two options, which gets at the core of your question. Now consider that someone else also worked on the same file as I did and we both give our changes to someone. How will this person know what we've done and if the changes are compatible (different parts of the file) or conflict (same lines of the file)? He will diff them! The diff can tell him how the files differ from each other and from the unmodified source file. If the diff is what is needed, it just makes more sense to just send the diff in the first place. A diff can also contain changes from more than one file, so while I edited 9 files in total, I can provide a single diff file to describe those changes.

Diffs can also be used to provide history. What if a change three months ago caused a bug I only discovered today. If I can narrow down when the bug was introduced and can isolate it to a specific change, I can use the diff to "undo" or revert the change. This is not something I could as easily do if I were only copying files around.

This all ties into source version control where programs may record files history as a series of diffs from the time it was created until today. The diffs provide history (I can recreate the file as it was on any particular day), I can see who to blame for breaking something (the diff has an owner) and I can easily submit changes to upstream projects by giving them specific diffs (maybe they are only interested in one change when I've made many).

In summary, yes, cp is easier than diff and patch, but the utility of diff and patch is greater than cp for situations where how files change is important to track.


When you get a patch you can often (that is unless you have made changes to the exact same lines) apply the patch to a set of files that you have changed yourself as well.

The patch has information about the old and the new state of the files. If you get a copied file you don't know what the original was (the old state) and you cannot apply the differences to a file (or set of files) that you have changed as well without great difficulty. So for sets of source files it is not the space preservation that is of major concern, it is the before-after information.

Before (context/unified) diffs this was often done with instructions for editors (insert a line after X, delete line Y), but that would only work if you knew the state these instruction started from. Thus having the same problem as your "solution" with just copying.


If you are using diff, you can see what exactly has changed, so using diff/patch is a way to prevent someone from slipping unwanted changes in the file.

Tags:

Shell

Diff

Patch

Cp