是否可以在diff文件中添加注释(统一)?

时间:2013-09-24 10:29:25

标签: git diff git-diff

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

对此的一个很好的用法是使用重要信息增加git diffs,例如从哪个分支到diff(特别是在使用--full-index选项时,它只显示blob引用)。

3 个答案:

答案 0 :(得分:43)

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

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

此标头之前的任何内容都会被忽略,因此您可以在此处添加任何评论,例如:

 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本身在标题之前为某些元数据使用此空格,例如:

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

答案 1 :(得分:11)

你可以在diff文件中使用#代替-+进行内联注释(GNU补丁可识别),或者在@JakubJirutka提到的开头添加注释< / p>

 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,

答案 2 :(得分:4)

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

 --- 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,