There is an article about a reprint of diff, to facilitate future review

This article is reprinted, in order to facilitate future review, specifically to record it. Others please go to the original address to watch! ! !
Article original address: http://www.ruanyifeng.com/blog/2012/08/how_to_read_diff.html


Read diff


Author: Ruan Yifeng


Date: 2012 August 29

diff Unix system is a very important tool program.

It is used to compare differences in two text files, it is one of the cornerstones code version management. You are at the command line, type:

```

  $ Diff <file before the change> <After change file>

```

diff will tell you, what difference these two files. It displays the results are less easy to understand, here I come to explain, how to read a diff.

A, diff three formats

Due to historical reasons, diff three formats:

```

  * Normal format (normal diff)

  * Format context (context diff)

  * Merge format (unified diff)

```

We turn to see.

Second, the sample files

For convenience of explanation, the first two examples of new files.

The first file called f1, content is one per line a, a total of seven lines.

```

  a
  a
  a
  a
  a
  a
  a

```

The second file is called f2, f1 modification is made, line 4 becomes b, the other unchanged.

```

  a
  a
  a
  b
  a
  a
  a

```

Third, the normal format of diff

Now f1 and f2 compare:

```

  $ diff f1 f2

```

In this case, diff will display the results of a normal format:

```

  4c4
  < a
  ---
  > b

```

The first line is a tips for position changes.

```

  4c4

```

It is divided into three parts: the front of "4" indicates 4 lines F1 of change; the intermediate "c" of variation is a schematic content change (Change), as well as other modes "increase" (A, on behalf of addition) and "delete" (D, on behalf Deletion); behind the "4", changes into a rear row of 4 F2.

The second row is divided into two portions.

```

  < a

```

Is less than the preceding number, which represents f1 removed from the row (i.e., line 4), the back of the "a" represents the contents of the row.

The third line used to separate f1 and f2.

```

  ---

```

The fourth line, similar to the second row.

```

  > b

```

F2 is greater than the preceding number indicates an increase of the line, behind the "b" represents the content of the line.

The earliest Unix (ie AT & T version of Unix), use is diff in this format.

Fourth, the context diff format

When the last century the early 1980s, the University of California, Berkeley launched BSD version of Unix, the results show that diff is too simple, it is best to join the context, easy to understand the changes that occur. Therefore, the introduction of a context diff format.

Its use is to add the parameter c (representing context).

```

  $ diff -c f1 f2

```

The results show the following:

```

  *** f1 2012-08-29 16:45:41.000000000 +0800
  --- f2 2012-08-29 16:45:51.000000000 +0800
  ***************
  *** 1,7 ****
   a
   a
   a
  !a
   a
   a
   a
  --- 1,7 ----
   a
   a
   a
  !b
   a
   a
   a

```

This result is divided into four parts.

Two rows of the first portion to display the basic case of two files: a file name and time information.

```

  *** f1 2012-08-29 16:45:41.000000000 +0800
  --- f2 2012-08-29 16:45:51.000000000 +0800

```

"***" indicates the file before the changes, "---" indicates that the file after such changes.

The second part is 15 asterisks, the basic situation and change the contents of files separated.

```

  ***************

```

The third section displays the file before the change, namely f1.

```

  *** 1,7 ****
   a
   a
   a
  !a
   a
   a
   a

```

In this case only the fourth display line is changed, and also shows three rows behind the front row of the three rows 4, thus displaying a total of 7 lines. So, in front of "*** 1,7 ****", says that from the beginning of the first line of seven consecutive lines.

In addition, each line of the front of the file contents, as well as a flag. If empty, said the bank did not change; if it is an exclamation point, said the bank has a change (!); If it is a minus sign (-) indicates that the row is deleted; if it is a plus sign (+) indicates that the new behavior .

The fourth section shows file changes, namely f2.

```

  --- 1,7 ----
   a
   a
   a
  !b
   a
   a
   a

```

In addition to changes in row (row 4) except that each display is the context of three rows, a total of seven rows displayed.

Notes to the consolidated format of diff

If the two files similarity is high, then the format of context diff, will show a large number of duplicate content, it is a waste of space. In 1990, GNU diff pioneered diff "Merge format", the context merge together f1 and f2 display.

Its use is added to the parameter u (representing unified).

```

  $ diff -u f1 f2

```

The results show the following:

```

  --- f1 2012-08-29 16:45:41.000000000 +0800
  +++ f2 2012-08-29 16:45:51.000000000 +0800
  @@ -1,7 +1,7 @@
   a
   a
   a
  -a
  +b
   a
   a
   a

```

Basic information of its first part, but also the file.

```

  --- f1 2012-08-29 16:45:41.000000000 +0800
  +++ f2 2012-08-29 16:45:51.000000000 +0800

```

"---" indicates that the file before the changes, "+++" indicates that the file after such changes.

A second portion, positional change of a First end and with two @.

```

  @@ -1,7 +1,7 @@

```

In front of "1,7" is divided into three parts: the first minus sign indicates a file (i.e., F1), "1" represents the first line, "7" denotes a continuous line 7. Taken together, the following is represented by a file starting from the first row of the first continuous line 7. Similarly, the "+ 1,7" indicates the change, to become the second file from the first row of seven consecutive start line.

The third part is the specific content changes.

```

   a
   a
   a
  -a
  +b
   a
   a
   a

```

In addition to those rows are subject to change, but also the context of each of the 3 lines. It context of two files, merge displayed together, so called "Merge format." Front of the flag of each line, empty means no change, a minus sign indicates row deleted files, plus sign to add a second file line.

Six, git diff format

Version control system git, using a variant of the combined diff format.

```

  $ git diff

```

The results show the following:

```

  diff --git a/f1 b/f1
  index 6f8a38c..449b072 100644
  --- a/f1
  +++ b/f1
  @@ -1,7 +1,7 @@
   a
   a
   a
  -a
  +b
   a
   a
   a

```

The first row shows the results for the git format diff.

```

  diff --git a/f1 b/f1

```

Comparison is performed, a version of f1 (i.e. before change) and b versions f1 (i.e., after the change).

The second line represents the two versions of git hash value (6f8a38c target area index, compared with 449b072 working directory target area), it is the last six digits mode (normal file, 644 authority) of the object.

```

  index 6f8a38c..449b072 100644

```

The third line represents two file comparison.

```

  --- a/f1
  +++ b/f1

```

"---" indicates the version before the change, "+++" indicates the version after such changes.

The same row with the official behind the merge format diff.

```

  @@ -1,7 +1,7 @@
   a
   a
   a
  -a
  +b
   a
   a
   a

```

Seven, reading material

  * diff - Wikipedia

  * How to read a patch or diff

  * How to work with diff representation in git

(Finish)

Guess you like

Origin www.cnblogs.com/Hesha/p/11263033.html