[learn and forget] git operation - 19. git diff command

1. git diff command description

Before commit ting, we usually need to determine where we changed the code and see if there is any misoperation code. At this time, the display of git status command is relatively simple, just listing the modified files. If you want to see what has been modified, you can use git diff command.

Show how many useful lines have changed: - stat.

2. Compare the differences between files in workspace and staging area

To see the difference between the contents of the workspace and the staging area, use the git diff command without options.

git diff file_name: get the modification of the specified file.

1) First, create a hello. Net in the working directory HTML file and add it to the staging area

# 1. Check the file status in the working directory
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

# 2. Create hello HTML file
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "hello git" > hello.html

# 3. Put hello Add HTML file to staging area
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add hello.html

2) To hello Add a new line to the HTML file, and then view the workspace and staging area hello Differences between HTML files

# 1. To hello Add content to HTML file
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "new one line" >> hello.html

# 2. Compare hello. In the workspace and staging area Differences between HTML files
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git diff hello.html
diff --git a/hello.html b/hello.html
index 8d0e412..ee5cc3c 100644
--- a/hello.html
+++ b/hello.html
@@ -1 +1,2 @@
 hello git
+new one line

explain:

  • diff --git a/hello.html b/hello.html: indicates that hello.html is used for comparison Version a (i.e. before the change) and version b (i.e. after the change) of HTML file
  • index 8d0e412..ee5cc3c: indicates the hash index values of two versions. The front side represents the index of the files in the temporary storage area, and the back side represents the index of the files in the workspace.
  • 100644: indicates the file mode, 100 represents the ordinary file, and 644 represents the permission of the file (the same as the Linux file permission).
  • --- a/hello.html and + + + B / Hello HTML: refers to two files for comparison, --- refers to the version before the change, + + + refers to the version after the change.
  • @@- 1 + 1,2 @ @: indicates the position of code change, with two @ as the beginning and end.
    Description with + 1,2: divided into three parts:
    +Represents the changed document, 1 represents the first line, and 2 represents two consecutive lines. (that is, there are two consecutive lines starting from the first line. My personal understanding is that there are several lines in the file.)
  • The last part is the specific content of document changes, and the flag at the front of each line:
    -The line representing the first file deletion is indicated in red.
    +Indicates the new line of the second file, represented in green.
    No flag indicates that there is no change in this line.

Here is a brief description of the function of the -- stat option, as follows:

# `--stat ` option function: concise display of differences
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git diff hello.html --stat
fatal: option '--stat' must come before non-option arguments
# Error prompt: the option "-- stat" must precede the non option parameter

# Write it correctly and only show the brief content
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git diff --stat hello.html
 hello.html | 1 +
 1 file changed, 1 insertion(+)

3) The modified hello Add the HTML file to the staging area and view the file again.

# 1. Change the modified hello Add HTML file to staging area
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add hello.html

# View hello. Com after executing the command HTML file
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git diff hello.html

There is no output, which means that at this time, hello The content of the HTML file is the same as hello.html in the temporary storage area There is no difference in the content of HTML files.

3. Compare the differences between the files in the staging area and the files in the local library

To see the difference between the temporary storage area and the file content in the local library, use the git diff command with the -- cached option.

Command: git diff --cached file_name

1) Follow the above exercise and put hello The HTML file is submitted to the local version library

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'add hello.html file'
[master 6b6b1fc] add hello.html file
 1 file changed, 2 insertions(+)
 create mode 100644 hello.html

2) Modify hello HTML file, and then add it to the staging area

# Modify hello HTML file
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ echo "new two two line" >> hello.html

# Add to staging area
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add hello.html

3) Compare hello. In the staging area and the local version library Differences between HTML files

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git diff --cached hello.html
diff --git a/hello.html b/hello.html
index ee5cc3c..7c88cdc 100644
--- a/hello.html
+++ b/hello.html
@@ -1,2 +1,3 @@
 hello git
 new one line
+new two two line

As can be seen from the above file, hello. Com in the staging area HTML file is better than hello.html in the local version library HTML file, one more line of new two two line content. (the interpretation method is the same as above.)

4. Summarize the common usage of git diff command

  1. Compare workspace to staging area:
    git diff command, without parameters, compares the working area and temporary storage area by default.
  2. Compare the staging area with the latest local version Library (the content of the latest commit in the local library):
    git diff --cached command or git diff --staged command (version 1.6.1 or above).
  3. Compare the workspace with the latest local version Library:
    git diff HEAD command, if HEAD points to the master branch, then HEAD can also be changed to master.
  4. Compare the differences between the workspace and the specified commit submission:
    Git diff commit ID command.
  5. Compare the differences between the staging area and the specified commit commit:
    Git diff -- cached commit ID command.
  6. Compare the differences between two commit submissions:
    Git diff [< commit ID >] [< commit ID >] command.
  7. Use the git diff command to patch. This usage will be explained in detail later. Just know it.

Tip: the above will not be explained in detail. The previous two examples are the same as others.

5. Summary

With the knowledge we have learned now, the git diff command can solve two problems:

  • Check which updates have not been staged yet?
    When you need to view the details, use the git diff command.
  • Check which updates have been temporarily stored and ready for next submission?
    When you need to view the details, use the git diff --cached command or git diff --staged command.

reference resources:

Keywords: git github

Added by dianaqt on Fri, 18 Feb 2022 02:26:12 +0200