1.git diff 用于查看未暂存的文件更新了哪些部分,也就是以修改未暂存的文件 和 已暂存的相比
1.创建文件diff.txt
echo blue01>diff.txt
2.git add把diff.txt添加到暂存区中
修改文件diff.txt内容为blue02,这时可以知道工作区中diff.txt内容为blue02,暂存区中diff.txt内容为blue01
3.git diff 比较工作区与暂存区
$ git diff diff.txt
warning: LF will be replaced by CRLF in diff.txt.
The file will have its original line endings in your working directory.
diff --git a/diff.txt b/diff.txt
index 6be2f5a..8c140fc 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1 +1 @@
-blue01 //暂存区
+blue02 //工作区
2.git diff --cached 用于暂存区和已经提交到分支上的进行比较
4.执行git diff --cached diff.txt 发现结果未空,也就是没有改变这个为什么呢?
原因是:这是工作区是blue02,暂存区是blue01,分支上也是blue01,因此暂存区与分支比较,没有改变的内容,如果想出现修改内容,则把工作区的blue02提交到暂存区即可。
5.git add diff.txt 提交到暂存区
6.执行git diff --cached diff.txt
$ git diff --cached diff.txt
diff --git a/diff.txt b/diff.txt
index 6be2f5a..8c140fc 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1 +1 @@
-blue01 //分支上
+blue02 //暂存区中
总结:git diff 工作区与暂存区对比,git diff --cached diff.txt 暂存区和仓库中分支做比较,(Git 1.6.1 及更高版本还允许使用 git diff --staged 代替 git diff --cached
,效果是相同的,但更好记些。)