Search code examples
gitdiffgit-diff

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


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).


Solution

  • 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:

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