git 是否可以向差异文件(统一)添加注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18979120/
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 09:06:14  来源:igfitidea点击:

is it possible to add a comment to a diff file (unified)?

gitdiffgit-diff

提问by fstab

I wonder if it's possible to add a certain amount of unparsed content to a diff file (unified) that is ignored as a comment.

我想知道是否可以将一定数量的未解析内容添加到作为注释被忽略的差异文件(统一)中。

One good use of this would be having git diffs augmented with important information such as from which branch is that diff from (especially when using the --full-index option, which merely displays the blob references).

一个很好的用途是让 git diffs 增加重要信息,例如来自哪个分支的差异(特别是在使用 --full-index 选项时,它只显示 blob 引用)。

回答by Jakub Jirutka

The unified diff starts with two line header:

统一差异以两行标题开头:

 --- from-file from-file-modification-time
 +++ to-file to-file-modification-time

Anything before this header is ignored, so you can add any comment here, for example:

忽略此标题之前的任何内容,因此您可以在此处添加任何注释,例如:

 This may be some useful description of this patch that
 will be ignored by the diff/patch utility.
 --- a/foo  2002-02-21 23:30:39.942229878 -0800
 +++ b/foo  2002-02-21 23:30:50.442260588 -0800
 @@ -1,7 +1,6 @@
 -The Way that can be told of is not the eternal Way;
 -The name that can be named is not the eternal name.
  The Nameless is the origin of Heaven and Earth;
 -The Named is the mother of all things.
 +The named is the mother of all things.
 +
  Therefore let there always be non-being,
    so we may see their subtlety,
  And let there always be being,

Git itself uses this space before header for some metadata, for example:

对于某些元数据,Git 本身在标头之前使用此空间,例如:

diff --git a/foo b/foo
index 59a4d1f..e48dfe7 100644
--- a/foo
+++ b/foo

回答by xuhdev

You can use #instead of -or +in the diff file for inline comments (recognizable by GNU patch), or add a comment at the begining mentioned by @JakubJirutka

您可以在 diff 文件中使用#代替-+用于内联注释(可被 GNU 补丁识别),或在@JakubJirutka 提到的开头添加注释

 This may be some useful description of this patch that
 will be ignored by the diff/patch utility.
 --- a/foo  2002-02-21 23:30:39.942229878 -0800
 +++ b/foo  2002-02-21 23:30:50.442260588 -0800
 @@ -1,7 +1,6 @@
 # comments on the deletion of the two lines
 -The Way that can be told of is not the eternal Way;
 -The name that can be named is not the eternal name.
  The Nameless is the origin of Heaven and Earth;
 -The Named is the mother of all things.
 +The named is the mother of all things.
 +
  Therefore let there always be non-being,
    so we may see their subtlety,
  And let there always be being,

回答by jkschneider

Anything after @@ on the same line is also ignored.

同一行@@ 之后的任何内容也将被忽略。

 --- a/foo  2002-02-21 23:30:39.942229878 -0800
 +++ b/foo  2002-02-21 23:30:50.442260588 -0800
 @@ -1,7 +1,6 @@ THIS IS A COMMENT THAT WILL BE IGNORED
 -The Way that can be told of is not the eternal Way;
 -The name that can be named is not the eternal name.
  The Nameless is the origin of Heaven and Earth;
 -The Named is the mother of all things.
 +The named is the mother of all things.
 +
  Therefore let there always be non-being,
    so we may see their subtlety,
  And let there always be being,