“git diff”中的“diff --git”输出指的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39834729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:19:15  来源:igfitidea点击:

What does the "diff --git" output in "git diff" refer to?

git

提问by JHZ

When I run git diff, the output begins with:

当我运行时git diff,输出开始于:

diff --git a/foo/bar b/foo/bar

If I try to run plain old diff --git, I'm told that the --gitoption doesn't exist (obviously, I guess, it would seem silly for a low-level tool to know about a specific DVCS). There's no mention of it in the manpage as well. Where does this come from?

如果我尝试运行普通 old diff --git,我会被告知该--git选项不存在(显然,我猜,对于了解特定 DVCS 的低级工具来说,这似乎很愚蠢)。man页面中也没有提到它。这是从哪里来的?

采纳答案by Keith Thompson

It's an "imaginary diff option", used to indicate to the reader that it's not justthe output of running the diffcommand. For example, in git's own git repo:

这是一个“虚构的差异选项”,用于向读者表明它不仅仅是运行diff命令的输出。例如,在 git 自己的 git repo 中:

$ git diff HEAD~1..HEAD | head
diff --git Documentation/git.txt Documentation/git.txt
index bd659c4..7913fc2 100644
--- Documentation/git.txt
+++ Documentation/git.txt
@@ -43,6 +43,11 @@ unreleased) version of Git, that is available from the 'master'
 branch of the `git.git` repository.
 Documentation for older releases are available here:

+* link:v2.10.0/git.html[documentation for release 2.10]
+
$ 

The diffcommand itself, if you invoked it with the same file name twice, would show no differences. gitpresumably creates temporary files corresponding to two different versions of Documentation/git.txtand feeds them to diff-- but the names of those temporary files would not be useful. I think git diffmassages the output of diffto make it more meaningful to the reader. (This speculation was not entirely correct. See below.)

diff命令本身,如果你使用相同的文件名调用它两次,会显示无显着差异。git大概创建对应于两个不同版本的临时文件Documentation/git.txt并将它们提供给diff- 但这些临时文件的名称没有用。我认为git diff按摩 的输出diff使其对读者更有意义。(这种推测并不完全正确。见下文。

Diving into the git source code, diff.chas the string "diff --git"hardwired as a string literal:

深入研究 git 源代码,diff.c将字符串"diff --git"硬连线为字符串文字:

strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);

And looking into the history of diff.cfor the earliest version that contains that string:

并查看diff.c包含该字符串的最早版本的历史:

$ git log -n 1 b58f23b3
commit b58f23b38a9a9f28d751311353819d3cdf6a86da
Author: Junio C Hamano <[email protected]>
Date:   2005-05-18 09:10:47 -0700

    [PATCH] Fix diff output take #4.

    This implements the output format suggested by Linus in
    <[email protected]>, except the
    imaginary diff option is spelled "diff --git" with double dashes as
    suggested by Matthias Urlichs.

    Signed-off-by: Junio C Hamano <[email protected]>
    Signed-off-by: Linus Torvalds <[email protected]>
$ 

Presumably <Pine.LNX...>is the message-id of some email message in a mailing list somewhere. In any case, this commit message makes it clear that diff --gitis an "imaginary diff option".

大概<Pine.LNX...>是某处邮件列表中某些电子邮件的消息 ID。无论如何,此提交消息清楚地表明这diff --git是一个“虚构的差异选项”。

This email message, cited by nos in a comment, appears to be part of the discussion that led to this.

这封电子邮件在评论中被 nos 引用,似乎是导致此问题的讨论的一部分。

UPDATE:I speculated above that git diffmassages the output of diff, adding this information. I just tried running git diffunder strace. It doesn't actually invoke the diffcommand, or any other command. Rather, all the output is printed by the gitprocess itself, and apparently it computes the differences internally. This behavior might also depend on the version of git(I'm using 2.23.0), the diffcommand(s) available, and the arguments used.

更新:我在上面推测会git diff按摩 的输出diff,添加此信息。我只是尝试git diffstrace. 它实际上并不调用diff命令或任何其他命令。相反,所有输出都由git进程本身打印,显然它在内部计算差异。这种行为也可能取决于git(我使用的是 2.23.0)的版本、diff可用的命令和使用的参数。

I'll note that GNU diff has a --label=LABELoption that couldbe used for this kind of thing:

我会注意到 GNU diff 有一个--label=LABEL选项可以用于这种事情:

'-L LABEL'
'--label=LABEL'
     Use LABEL instead of the file name in the context format (*note
     Context Format::) and unified format (*note Unified Format::)
     headers.  *Note RCS::.

but git diffdoesn't use it, at least in the one case I tried, and I don't see a reference to it in the git sources.

git diff不使用它,至少在我尝试过的一种情况下,我在 git 源代码中没有看到对它的引用。

回答by Thomas Ayoub

The --gitis to mean that diff is in the "git" diff format. It doesn't refers to and option of the /usr/bin/diffcommand You can find the list of diff formatdocumentation. Other formats are:

--git意味着差异采用“git”差异格式。它不涉及/usr/bin/diff命令的和选项您可以找到diff 格式文档的列表。其他格式有:

  • diff --combined
  • diff --cc
  • diff --summary
  • diff --combined
  • diff --cc
  • diff --summary

回答by Walf

As Keith mentioned, in GNU diff (check your version with diff -v), you can have git-style patches by using --labellike so:

正如 Keith 所提到的,在 GNU diff 中(用 来检查你的版本diff -v),你可以--label像这样使用 git 风格的补丁:

a single file from a backup

备份中的单个文件

diff -u --label a/path/to/file.ext --label b/path/to/file.ext path/to/file.ext~ path/to/file.ext

or from STDIN

或来自标准输入

command_that_outputs_previous_version | diff -u --label a/path/to/file.ext --label b/path/to/file.ext - path/to/file.ext

You can use those commands in a loop of files from find, etc. Where one side of a comparison does not exist, e.g. new files, compare with /dev/null.

您可以在来自find等的文件循环中使用这些命令。如果比较的一侧不存在,例如新文件,则与/dev/null.