What does the “@@…@@” meta line with at signs in svn diff or git diff mean?

When I use svn diff or git diff it shows lines like:

@@ -1,5 +1,9 @@

What do they mean?

  • How to rename an SVN branch and update references in an existing sandbox?
  • Don't show .svn folders in Eclipse
  • How to migrate all URLs in svn:externals properties across a repository?
  • Netbeans and SVN UUID problem
  • BitBucket colored diff email service
  • Split SVN repository into several repositories
  • Why am I getting the error on svn client “XML parse error at line 1 no element found” when accessing VisualSVN Server?
  • How do I download code using SVN/Tortoise from Google Code?
  • 3 Solutions collect form web for “What does the “@@…@@” meta line with at signs in svn diff or git diff mean?”

    Those are called (c)hunk headers and contain the range information.

    They are surrounded by double at signs @@. They are of the format:

    @@ -l,s +l,s @@
    

    where l is the starting line number and s is the number of lines the change (c)hunk applies to for each respective file. The - indicates the original file and the + indicates the new (modified) file. Note that it not only shows affected lines, but also context lines.

    The -1,5 is in the original file (indicated by the -). It shows that that first line is the start and 5 affected / context lines

    The +1,9 is in the new (modified) file (indicated by the +) and again first line is the start and 9 affected / context lines.

    More details here: http://en.wikipedia.org/wiki/Diff#Unified_format

    Simple example analysis

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

    For instance:

    diff -u <(seq 16) <(seq 16 | grep -Ev '^(2|3|14|15)$')
    

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

    @@ -1,6 +1,4 @@
     1
    -2
    -3
     4
     5
     6
    @@ -11,6 +9,4 @@
     11
     12
     13
    -14
    -15
     16
    

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

    • -1,6: this piece corresponds to line 1 to 6 of the first file:

      1
      2
      3
      4
      5
      6
      

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

    • +1,4 says that this piece corresponds to line 1 to 4 of the second file.

      + means “new”.

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

      1
      4
      5
      6
      

    @@ -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?

    These describe the lines affected by the diff hunk. In your case, it means the hunk affects 5 lines starting from line 1, resulting in a replacement starting at line 1 which is 9 lines long.

    Note that this is the format used by the unified diff format. The “classical” diff format uses a different model (but who uses classical diff these days?).

    Git Baby is a git and github fan, let's start git clone.