场景

修改仓库文件提交到暂存区后还未commit,此时再次对仓库文件进行修改,比较修改后工作目录文件和暂存区文件差异。

git diff: 后面不加具体文件则默认比较工作区和暂存区所有文件的区别

git diff -- [具体文件1] [具体文件2]: 查看工作区和暂存区中具体文件的变动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 G:\mygitea\GitLearn\learn03   master ± 
$ git diff
diff --git a/readme b/readme
index edd02cd..4dd1fab 100644
--- a/readme
+++ b/readme
@@ -4,3 +4,4 @@ This is the young branch.
after test, update readme.
test HEAD and cached compare fix 2.
2
+123445
diff --git a/test.txt b/test.txt
index f29dee6..4debe7e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
测试
+222

G:\mygitea\GitLearn\learn03   master ± 
$ git diff -- readme
diff --git a/readme b/readme
index edd02cd..4dd1fab 100644
--- a/readme
+++ b/readme
@@ -4,3 +4,4 @@ This is the young branch.
after test, update readme.
test HEAD and cached compare fix 2.
2
+123445

总结

1
2
3
4
假定:HEAD、缓存区、工作区中的readme.md文件内容均不相同。
git diff HEAD -- readme.md # 工作区 <===> HEAD
git diff -- readme.md # 工作区 <===> 缓存区
git diff --cached -- readme.md # 缓存区 <===> HEAD

Q1:Git diff — file. 这里的—是什么意思,没有也能正常工作?

— 为了让git命令读取命令参数的时候消除歧义用的,双连字符后面的是路径或文件