diff 和 git diff 创建的 patches/files 有什么区别?

What differences are in the patches/files created by diff and git diff?

我想知道 diffgit-diff 创建的 files/patches 的格式有什么不同。
我知道在 diff 中有 3 个("normal"、"compact" -c 和 "unified" -u 一个)。

然而可能存在一些差异,在某些情况下您不能自由交换 git diffdiff。所以:

  1. 是否可以对同一个文件使用git diffdiff取决于什么?
  2. 格式之间有什么区别?
  3. 如果不能交换命令(参见 1.),如何将文件转换为其他格式以便可以将它们与其他命令一起使用?
  4. 如果可以交换命令(见 1.):是否推荐这样做?
  5. 这两个命令创建的文件还有其他显着差异吗?
  1. On what does it depend whether you can use git diff and diff for the same file?

只要文件在 git 存储库工作树中,那么您就可以使用 git diff 来显示该文件的更改(针对 git 回购,如索引或 blob objects).
这与'diff'不同,它比较文件(这意味着你需要两个文件,而不是像git diff 中使用的 git回购)

hvd points out in the comments:

You can use git diff outside any work tree and pass it two files.
So you can use git diff in pretty much any situation you can use diff.
The reverse is not true

 git diff --color-words --no-index file1 file2

  1. And what are the differences between the formats?

git diff 可以模拟任何 diff 格式(统一,原始,...)。
它也有 git-specific 格式 (--summary, --stat, ...)

另请参阅:

A git diff 将包含 git header,以及 "similarity index".
每个差异块的 hunks 显示为 very similar to a diff -u.


  1. If you cannot exchange the commands (see 1.) how can you convert the files into the other format so that you can use them with the other command?

您可以将 git diff 转换为原始格式,或使用原始格式进行修补:--patch-with-raw.
反过来是可能的:you can apply a diff to a git repo.


  1. If you can exchange the commands (see 1.): Is it even recommend to do so?

如果您没有安装 git(参见 the previous example


  1. Are there any other notable differences in the files created by the two commands?

否:应用 diffgit diff 生成的补丁的结果应该是相同的。