差异是什么补丁由diff和gitdiff/文件? - What differences are in the patches/files created by diff and git diff?

- 此内容更新于:2016-02-03
主题:

我想知道不同的格式和创建的文件/补丁。我知道在diff有3(“正常”、“紧凑”和“统一”)。但是可能会有一些差异,在某些情况下你不能自由交流和diff。如此:它依赖是否可以使用gitdiffanddiff相同的文件吗?格式之间的差异是什么?如果你不能交换的命令(见1。)你怎么能把文件转换成其他格式,以便您可以使用它们与其他命令吗?如果你能交换的命令(见1。):它甚至推荐这样做吗?还有其他的显著差异的两个命令创建的文件吗?

原文:

I wonder what differences are in the format of the files/patches created by diff and git-diff.
I know in diff there are 3 (the "normal", the "compact" -c one and the "unified" -u one).

However there may be some differences and in some situations you cannot freely exchange git diffand diff. So:

  1. On what does it depend whether you can use git diffand diff for the same file?
  2. And what are the differences between the formats?
  3. 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?
  4. If you can exchange the commands (see 1.): Is it even recommend to do so?
  5. Are there any other notable differences in the files created by the two commands?
网友:所有的这些都是记录在git参考文档(见git-scm.com/docs/git-diff)。约git-diff添加一些更多的头在某些情况下。

(原文:All of this is documented in the git reference documentation (see git-scm.com/docs/git-diff). Roughly git-diff adds some more headers on some situations.)

解决方案:
它取决于是否可以使用相同的文件?如果文件是一个git存储库工作树,那么你可以使用gitdiff来改变该文件(针对相同的文件所引用的git存储库时,该指数或blob对象)。这不同于“diff”,对比文件(意味着你需要两个文件,不只是一个在gitdiff当用于一个git存储库)为hvd在评论中指出:您可以使用gitdiff以外的任何工作树,通过这两个文件。所以你可以使用gitdiff在任何情况下您可以使用diff。相反的是不正确的格式之间的差异是什么?gitdiff可以模拟任何diff格式(统一,生,…)。它也git-specific格式(,,…)参见:“如何阅读从gitdiff输出?“一个gitdiff将包括一个git头、“相似性指数”。每个块的大块显示差别非常类似于diff-u。如果你不能交换的命令(见1。)你怎么能把文件转换成其他格式,以便您可以使用它们与其他命令吗?你能将一个gitdiff原始格式,或与原始:补丁。相反的是可能的:你可以把一个git存储库的差异。如果你能交换的命令(见1。):它甚至推荐这样做吗?这是如果你没有git安装(参见前面的示例)还有其他的显著差异的两个命令创建的文件吗?没有:应用补丁的结果所产生的差异或gitdiff应该是相同的。
原文:
  1. On what does it depend whether you can use git diff and diff for the same file?

Simply if the file is in a git repo working tree, then you would be able to use git diff to show changes for that file (against the same file as referenced by the git repo, like the index or blob objects).
This differs from 'diff', which compares files (meaning you need two files, not just one as in git diff when used in a git repo)

As 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 can emulate any diff format (unified, raw, ...).
It has git-specific format as well (--summary, --stat, ...)

See also:

A git diff will include a git header, with a "similarity index".
The hunks displays for each chunk of diffs are 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?

You can convert a git diff in a raw format, or patch with raw: --patch-with-raw.
The reverse is possible: 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?

It is if you don't have git installed (see the previous example)


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

No: the result of applying a patch generated by a diff or a git diff should be the same.

网友:您可以使用任何工作以外的树并将其传递给两个文件。所以你可以使用gitdiff在任何情况下您可以使用。相反的是不正确的,你的答案你已经提到过。

(原文: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, as you rightly mentioned in your answer.)

网友:@hvd真的。我包括你的评论、链接和例子,答案更多的可见性。

(原文:@hvd True. I have included your comment, with link and example, in the answer for more visibility.)