git diff的一些区别

258 阅读1分钟

1、简介

首先要知道3种状态:

  • working tree:就是在工作区进行修改,但没git add的那种状态

  • index file:工作区修改完以后使用了git add但没git commit的状态

  • commit:顾名思义,就是提交了(git commit了)

那现在再理解一下这三个指令的含义:

  • git diff:比较working tree和index file的状态
  • git diff HEAD:比较working tree和commit的状态
  • git diff --cached:比较index和commit的状态

img

2、实例

比较git diff:

vim a.txt ==> 写a=1
vim b.txt ==> 写b=1
vim c.txt ==> 写c=1

#然后都git add .

#接着对a.txt进行修改 ==> 新增a=2
#输入git diff
$ git diff
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
diff --git a/a.txt b/a.txt
index 73cdb8b..23d06d6 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
 a=1
+a=2

比较git diff HEAD:

#把这些commit了,再重新创建个文件
git add .

git commit --amend

#接着对a.txt进行修改 ==> 新增a=3

git diff HEAD

比较git diff --cached

vim b.txt

git diff --cached