git diff used to check changes

Use diff to check changes

Development of the project is composed of numerous tiny changes in the composition. The key to understand the project development process is to figure out every change.
Of course, you can use "git status" command or more simply "git log" command to print out the status and history of the project, but these commands can only provide a very brief summary of information you want to display a detailed modify the information you must use another command.

Read Diffs

In the version control system to display the differences between two versions of the operation we call "diff", or "patch". Now let us learn in detail about the operation of it! First, learn how to read a diff information.

Comparison file a / b

The diff will operate two objects compared with each other. For example objects A and B. In most cases A and B will be the same file in the project, but they are based on different versions. Of course, diff operations compare two files may be completely unrelated, and to show the difference between them, but this operation is not frequently used.
In order to clearly show the comparison information, diff operation will always files to be compared is defined as "A" and "B".

Metadata file (Metadata)

It said file metadata is very technical, in practice, you may never need to thoroughly understand it. Two strings of numbers represent the beginning of hashes of the two files (simple point that is their "ID"). Not just the project, Git will put each file as an object to save. The hash ID represents a specific version of a file object. The last string of numbers represents the mode of a file (100644 behalf it is an ordinary file, an executable file represents 100,755, 120,000 is just a symbolic link).

Marking a / b

Continue to observe the output down, the real difference between A and B will be displayed here. To distinguish them, A and B have been given their specific symbols: for version A, which is a minus sign ( "-"); and for version B, it uses a plus sign ( "+").

Block (Chunk)

diff action will not display the full contents of the file. If the difference between the two versions exist only two lines of code, you will not want to go line by line look at this file with 10,000 lines of code in it. Thus, Git here only those parts marked actually modified, where modification is called a contiguous block (or the chunk hunk). In addition to including the actual lines of code changes, one block also includes a specific "context", such as those differences before and after the change, to make it easier to understand the specific meaning of this change in a particular context.

Header block (Chunk Header)

Each such block has a header, which is displayed in the two "@@" symbol. Here Git will tell you which lines are different. In our example, these lines are marked to be the first block changes:

  • From document A (marked "-"), after the start of line 6 from line 34.
  • From document B (labeled "+"), after the start of line 8 from line 34.

Information after the "@ @" symbol is used to indicate the end of context, such as Git will try to assign a name or any other method of contextual information for the block. However, Git can not support all of the contents of the documents, which largely depends on the development language must be used by the project.

change

Before each of which is altered in a front line of code will be "+" or "-" sign. As previously mentioned, these symbols can help you understand exactly version A and version B. For example, the front "-" symbol represents the line from version A, and vice versa with the symbol "+" represents a row from the version B.
In most cases, the Git are used in such a way as A and B, you can think of A / - represents the old content, and B / + on behalf of the new content.

Now let's look at our example:

  • Changes # 1 includes two lines of "+", but does not exist in the corresponding versions of A in these rows (no is preceded by the "-" line), which means the two lines are newly added.
  • # 2 changes the contrary. In version A, you can see there are two lines on the front of the sign "-." However, version B does not exist corresponding line (no "+" line), suggesting that these two lines are deleted.
  • In the modification # 3, these lines have been some changes, the preamble symbol "-" in the two rows is modified, a new change is below it is marked "+" symbol content.

Now that we know how to read the output of the diff, doing some exercises now!

Check the local changes

In the previous chapters, we often use "git status" command to see which files have been altered in a local copy (working copy) in. If you want a clear understanding of the details of these changes, we have to use "git diff" command:

$ git diff
diff --git a/about.html b/about.html
index d09ab79..0c20c33 100644
--- a/about.html
+++ b/about.html
@@ -19,7 +19,7 @@
   </div>

   <div id="headerContainer">
-    <h1>About&lt/h1>
+    <h1>About This Project&lt/h1>
   </div>

   <div id="contentContainer">
diff --git a/imprint.html b/imprint.html
index 1932d95..d34d56a 100644
--- a/imprint.html
+++ b/imprint.html
@@ -19,7 +19,7 @@
   </div>

   <div id="headerContainer">
-    <h1>Imprint&lt/h1>
+    <h1>Imprint / Disclaimer&lt/h1>
   </div>

   <div id="contentContainer">

Without any arguments, "git diff" will be compared to all the local copy has not yet been packaged (unstaged) changes for us, and displayed.
If you just want to see the result of the comparison for those changes have been packaged, you can choose to use "git diff --staged" command.

Check the changes submitted

You have learned a "git log" command, it will print out the latest summary of those submitted. But it only shows some of the most basic information (hash, author, time, comments). If you want to see the details of those changes, you can add "-p" argument to get a more detailed and correct information.

Compare and branch version

Finally, you may want to know how to compare the two branches, or two versions of a particular project. To make us look at the "contact-form" which changes the branch does not exist in the "master":

$ git diff master..contact-form

Instead, these are only comparative information on the content between the branch level, you can compare any two versions of the project:

$ git diff 0023cdd..fcd6199

Guess you like

Origin www.cnblogs.com/qiumingcheng/p/11233811.html