背景

在git仓库中,我们将暂存区的文件提交前还需要判断暂存区文件和HEAD所含文件的差异,判断是否能够作为新的提交。

实践

我们修改git仓库的readme文件:

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
 G:\mygitea\GitLearn\learn03   master ± 
$ vim readme

G:\mygitea\GitLearn\learn03   master ± 
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme

no changes added to commit (use "git add" and/or "git commit -a")

G:\mygitea\GitLearn\learn03   master ± 
$ git add readme

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

此时比较后如果发现存在问题,我们还可以继续修改暂存中的文件,之后再提交。

总结

git diff —-staged 和 git diff -—cached 是一样的,stage和cache都指暂存区。

git diff --cached后面可以指定文件名的。不加就是比对所有文件差异。