47

Could somebody explain me the difference between a .diff file and .patch file.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82

4 Answers4

26

What matters is the content of the file, not the extension. Both of those extensions imply that some sort of diff utility (diff, git diff, git format-patch, svn diff) produced the output.

Many diff utilities produce output which can be applied by the patch command. You will frequently need to use the -d and -p options to patch in order to get the paths matched up right (strip prefix, name target directory). If you see one of those extensions on a file distributed online, it's almost certainly an indication it's compatible with patch.

Git's diff output is compatible with patch, but I believe svn's is not. Of course, plain patches generated by git diff are probably best applied by git apply, and patches generated by git format-patch are designed for use with git-am.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
10

patch is a unified diff (-u), if you do a: diff -u oldfile newfile, with patch command line, you can apply the differences to oldfile to become newfile somewhere else.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Sérgio
  • 6,966
  • 1
  • 48
  • 53
  • 1
    I like this idea. Patches being a collection of diffs. I wasn't looking for a "correct extension" but a maybe a standard practice for naming. Whether or not this is in common use, it makes sense to me. I'll go forward using .diff for single file diffs and .patch for multiple consolidated diffs. – Chad M Mar 31 '16 at 02:46
7

There are no differences. diff utility produces a patch file which is applied using patch.

khachik
  • 28,112
  • 9
  • 59
  • 94
  • 2
    That is to say, when you use `diff` to create the patch, you call it foo.diff or foo.patch (or anything else). The `patch` program doesn't care. – eaj Nov 18 '10 at 14:33
  • so if a diff file is given and it is said that you should use it for patching the source, what does it imply. Can i use the patch -d -p[n] command to patch it? – Navaneeth Sen Nov 18 '10 at 14:34
  • 2
    @Sen: "Can I use `patch`...?" The best answer there is "try it and see". If the patch doesn't apply, or `patch` can't read it, you'll get errors. If it works, it works. – Cascabel Nov 18 '10 at 14:42
  • `-d ... -p` are to avoid creating the same directory structure as where the patch has been generated. e.g. if the patch file contains `/home/user/projects/p1/prog.c` and needs to be applied to `/home/anotheruser/work/project1/prog.c`, `patch -d /home/anotheruser/work/project – khachik Nov 18 '10 at 14:49
3

For me .diff files contain file differences only (added/deleted lines) that are produced by "git diff"

.patch files for me also contain the author, date and patch/commit message, as generated by git show.


Github allows to add .patch and .diff to pull request URLs. The .diff contains only the file changes, while the .patch is a serialization of all commits in that PR, with the commit message and file content diff in each.

cweiske
  • 30,033
  • 14
  • 133
  • 194