“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
What does the "diff --git" output in "git diff" refer to?
提问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 --git
option 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 man
page 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 diff
command. 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 diff
command itself, if you invoked it with the same file name twice, would show no differences. git
presumably creates temporary files corresponding to two different versions of Documentation/git.txt
and feeds them to diff
-- but the names of those temporary files would not be useful. I think git diff
massages the output of diff
to 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.c
has 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.c
for 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 --git
is 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 diff
massages the output of diff
, adding this information. I just tried running git diff
under strace
. It doesn't actually invoke the diff
command, or any other command. Rather, all the output is printed by the git
process 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 diff
command(s) available, and the arguments used.
更新:我在上面推测会git diff
按摩 的输出diff
,添加此信息。我只是尝试git diff
在strace
. 它实际上并不调用diff
命令或任何其他命令。相反,所有输出都由git
进程本身打印,显然它在内部计算差异。这种行为也可能取决于git
(我使用的是 2.23.0)的版本、diff
可用的命令和使用的参数。
I'll note that GNU diff has a --label=LABEL
option 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 diff
doesn'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 --git
is to mean that diff is in the "git" diff format. It doesn't refers to and option of the /usr/bin/diff
command 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 --label
like 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
.