问题描述
git-diff
的手册页相当长,并且解释了许多初学者似乎不需要的情况.例如:
The man page for git-diff
is rather long, and explains many cases which don't seem to be necessary for a beginner. For example:
git diff origin/master
推荐答案
让我们看一下来自 git 历史的示例高级差异(在 在 git.git 存储库中提交 1088261f):
Lets take a look at example advanced diff from git history (in commit 1088261f in git.git repository):
diff --git a/builtin-http-fetch.c b/http-fetch.c
similarity index 95%
rename from builtin-http-fetch.c
rename to http-fetch.c
index f3e63d7..e8f44ba 100644
--- a/builtin-http-fetch.c
+++ b/http-fetch.c
@@ -1,8 +1,9 @@
#include "cache.h"
#include "walker.h"
-int cmd_http_fetch(int argc, const char **argv, const char *prefix)
+int main(int argc, const char **argv)
{
+ const char *prefix;
struct walker *walker;
int commits_on_stdin = 0;
int commits;
@@ -18,6 +19,8 @@ int cmd_http_fetch(int argc, const char **argv, const char *prefix)
int get_verbosely = 0;
int get_recover = 0;
+ prefix = setup_git_directory();
+
git_config(git_default_config, NULL);
while (arg < argc && argv[arg][0] == '-') {
让我们逐行分析这个补丁.
Lets analyze this patch line by line.
第一行
The first line
diff --git a/builtin-http-fetch.c b/http-fetch.c
是一个git diff";格式为 diff --git a/file1 b/file2
的标题.a/
和 b/
文件名是相同的,除非涉及重命名/复制(就像我们的例子).--git
表示 diff 位于git"目录中.差异格式.
is a "git diff" header in the form diff --git a/file1 b/file2
. The a/
and b/
filenames are the same unless rename/copy is involved (like in our case). The --git
is to mean that diff is in the "git" diff format.
接下来是一个或多个扩展的标题行.前三
Next are one or more extended header lines. The first three
similarity index 95%
rename from builtin-http-fetch.c
rename to http-fetch.c
告诉我们文件从 builtin-http-fetch.c
重命名为 http-fetch.c
并且这两个文件 95% 相同(用于检测此重命名).
扩展差异标头中的最后一行,即
tell us that the file was renamed from builtin-http-fetch.c
to http-fetch.c
and that those two files are 95% identical (which was used to detect this rename).
The last line in extended diff header, which is
index f3e63d7..e8f44ba 100644
告诉我们有关给定模式的信息文件(100644
表示它是普通文件而不是符号链接,并且它没有可执行权限位),以及关于原像(给定更改前的文件版本)和后图像的缩短哈希(更改后的文件版本).git am --3way
使用此行在无法应用补丁时尝试进行三向合并.
tell us about mode of given file (100644
means that it is ordinary file and not e.g. symlink, and that it doesn't have executable permission bit), and about shortened hash of preimage (the version of file before given change) and postimage (the version of file after change). This line is used by git am --3way
to try to do a 3-way merge if patch cannot be applied itself.
接下来是两行统一的diff header
Next is two-line unified diff header
--- a/builtin-http-fetch.c
+++ b/http-fetch.c
与 diff -U
结果相比,它在源之后没有 from-file-modification-time 和 to-file-modification-time (原图像)和目标(后图像)文件名.如果文件被创建,源是 /dev/null
;如果文件被删除,目标是 /dev/null
.
如果你将 diff.mnemonicPrefix
配置变量设置为 true,代替 a/
和 b/
前缀在这个两行标题中,您可以使用 c/
, i/
, w/
和 o/
作为前缀,分别作为您比较的内容;参见 git-config(1)
Compared to diff -U
result it doesn't have from-file-modification-time nor to-file-modification-time after source (preimage) and destination (postimage) file names. If file was created the source is /dev/null
; if file was deleted, the target is /dev/null
.
If you set diff.mnemonicPrefix
configuration variable to true, in place of a/
and b/
prefixes in this two-line header you can have instead c/
, i/
, w/
and o/
as prefixes, respectively to what you compare; see git-config(1)
接下来是一个或多个差异;每个大块显示文件不同的一个区域.统一格式大块以像
Next come one or more hunks of differences; each hunk shows one area where the files differ. Unified format hunks starts with line like
@@ -1,8 +1,9 @@
或
@@ -18,6 +19,8 @@ int cmd_http_fetch(int argc, const char **argv, ...
格式为@@ from-file-range to-file-range @@ [header]
.形式-,
,to-file-range为+,
.start-line 和 number-of-lines 分别是指原像和后像中大块的位置和长度.如果行数不显示,则表示为 1.
It is in the format @@ from-file-range to-file-range @@ [header]
. The from-file-range is in the form -<start line>,<number of lines>
, and to-file-range is +<start line>,<number of lines>
. Both start-line and number-of-lines refer to position and length of hunk in preimage and postimage, respectively. If number-of-lines not shown it means that it is 1.
可选标头显示每次更改发生的 C 函数,如果它是 C 文件(如 GNU diff 中的 -p
选项),或其他类型文件的等效项(如果有).
The optional header shows the C function where each change occurs, if it is a C file (like -p
option in GNU diff), or the equivalent, if any, for other types of files.
接下来是文件不同之处的描述.两个文件共有的行以空格字符开头.两个文件之间实际不同的行在左侧打印列中具有以下指示符之一:
Next comes the description of where files differ. The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:
'+' -- 在第一个文件中添加了一行.
'+' -- A line was added here to the first file.
'-' -- 这里从第一个文件中删除了一行.
'-' -- A line was removed here from the first file.
例如,第一个块
So, for example, first chunk
#include "cache.h"
#include "walker.h"
-int cmd_http_fetch(int argc, const char **argv, const char *prefix)
+int main(int argc, const char **argv)
{
+ const char *prefix;
struct walker *walker;
int commits_on_stdin = 0;
int commits;
表示 cmd_http_fetch
被 main
替换,并且添加了 const char *prefix;
行.
means that cmd_http_fetch
was replaced by main
, and that const char *prefix;
line was added.
换句话说,在更改之前,'builtin-http-fetch.c' 文件的相应片段如下所示:
In other words, before the change, the appropriate fragment of then 'builtin-http-fetch.c' file looked like this:
#include "cache.h"
#include "walker.h"
int cmd_http_fetch(int argc, const char **argv, const char *prefix)
{
struct walker *walker;
int commits_on_stdin = 0;
int commits;
更改后,现在'http-fetch.c'文件的这个片段看起来像这样:
After the change this fragment of now 'http-fetch.c' file looks like this instead:
#include "cache.h"
#include "walker.h"
int main(int argc, const char **argv)
{
const char *prefix;
struct walker *walker;
int commits_on_stdin = 0;
int commits;
- 可能存在
- There might be
No newline at end of file
line (它不在示例差异中).
line present (it is not in example diff).
- There might be
- git-diff(1) 联机帮助页,使用 -p 生成补丁"部分;
- (diff.info)详细统一 节点,统一格式的详细说明".
- git-diff(1) manpage, section "Generating patches with -p"
- (diff.info)Detailed Unified node, "Detailed Description of Unified Format".
正如Donal Fellows 所说最好在现实生活中练习阅读差异,在那里你知道你改变了什么.
As Donal Fellows said it is best to practice reading diffs on real-life examples, where you know what you have changed.
参考:
这篇关于如何读取 git diff 的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!