Git 命令之Git diff

git diff 最直观的理解和印象是比较差异

  • git diff 

  1. 当工作区有变化,并且没有执行add 操作时,显示的是工作区和 最后提交的commit 的共同文件的之间的差别 

    Git 命令之Git diff_第1张图片

     这里添加了一行代码,没有 git add ,然后执行git diff ,结果如下

    diff --git a/D.java b/D.java
    index 4c5e244..4da3cdc 100644
    --- a/D.java
    +++ b/D.java
    @@ -19,5 +19,6 @@ class D {
                 System.out.print(i+" ");
             }
             System.out.println();
    +        System.out.println("Hello world");
         }
     }
    \ No newline at end of file
  2. 工作区有改动,临时区不为空,即已经git add ,git diff 显示的是工作区和暂存区的共同文件之间的差别

          

git add 

 之后执行git diff ,内容无差别

git diff 

  • git diff --cached 或者 git diff --staged         

此时已经add 了, 文件进入了暂存区 ,这里展示的是暂存区和上次的commmit id 的所有的不相同文件的差别和变化,包括一些增删改

diff --git a/D.java b/D.java
index 4c522e4..4da3cdc 100644
--- a/D.java
+++ b/D.java
@@ -19,5 +19,6 @@ class D {
             System.out.print(i+" ");
         }
         System.out.println();
+        System.out.println("Hello world");
     }
 }
\ No newline at end of file

  • git diff HEAD     

工作区 —>add 暂存区 —>commit 本地版本库

以上命令显示 工作区、 暂存区、上一次的commit 之间的所有不相同文件的增删改的变化

>>git status

On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   D.java

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   C.txt

>> git diff 

diff --git a/C.txt b/C.txt
index 51md0e5..7f2f90e 100644
--- a/C.txt
+++ b/C.txt
@@ -1,3 +1,6 @@
 
 
 add C.txt
+
+
+hello
\ No newline at end of file
diff --git a/D.java b/D.java
index 4c55244..4datcdc 100644
--- a/D.java
+++ b/D.java
@@ -19,5 +19,6 @@ class D {
             System.out.print(i+" ");
         }
         System.out.println();
+        System.out.println("Hello world");
     }
 }
\ No newline at end of file

你可能感兴趣的:(Git,笔记,git,java,服务器)