What does “@@ -1 +1 @@” mean in Git's diff output?

前端 未结 3 1279
后悔当初
后悔当初 2020-11-29 16:51

I\'ve been collecting data from the information returned from

git diff ..

and I ran into @@ -1 +1 @@<

3条回答
  •  盖世英雄少女心
    2020-11-29 17:30

    Simple example analysis

    The format is basically the same the diff -u unified diff.

    For instance:

    diff -u <(seq -w 16) <(seq -w 16 | grep -Ev '^(02|03|14|15)$')
    

    Here we removed lines 2, 3, 14 and 15. Output:

    @@ -1,6 +1,4 @@
     01
    -02
    -03
     04
     05
     06
    @@ -11,6 +9,4 @@
     11
     12
     13
    -14
    -15
     16
    

    @@ -1,6 +1,4 @@ means:

    • -1,6 means that this piece of the first file starts at line 1 and shows a total of 6 lines. Therefore it shows lines 1 to 6.

      1
      2
      3
      4
      5
      6
      

      - means "old", as we usually invoke it as diff -u old new.

    • +1,4 means that this piece of the second file starts at line 1 and shows a total of 4 lines. Therefore it shows lines 1 to 4.

      + means "new".

      We only have 4 lines instead of 6 because 2 lines were removed! The new hunk is just:

      01
      04
      05
      06
      

    @@ -11,6 +9,4 @@ for the second hunk is analogous:

    • on the old file, we have 6 lines, starting at line 11 of the old file:

      11
      12
      13
      14
      15
      16
      
    • on the new file, we have 4 lines, starting at line 9 of the new file:

      11
      12
      13
      16
      

      Note that line 11 is the 9th line of the new file because we have already removed 2 lines on the previous hunk: 2 and 3.

    Hunk header

    Depending on your git version and configuration, you can also get a code line next to the @@ line, e.g. the func1() { in:

    @@ -4,7 +4,6 @@ func1() {
    

    This can also be obtained with the -p flag of plain diff.

    Example: old file:

    func1() {
        1;
        2;
        3;
        4;
        5;
        6;
        7;
        8;
        9;
    }
    

    If we remove line 6, the diff shows:

    @@ -4,7 +4,6 @@ func1() {
         3;
         4;
         5;
    -    6;
         7;
         8;
         9;
    

    Note that this is not the correct line for func1: it skipped lines 1 and 2.

    This awesome feature often tells exactly to which function or class each hunk belongs, which is very useful to interpret the diff.

    How the algorithm to choose the header works exactly is discussed at: Where does the excerpt in the git diff hunk header come from?

提交回复
热议问题