git diff 命令

Posted xiaofei001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git diff 命令相关的知识,希望对你有一定的参考价值。

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,效果是相同的,但更好记些。)

以上是关于git diff 命令的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

修复git diff正文中文乱码

git diff 命令

git个人使用总结(命令版)

能否在所有git项目中,让所有的 "git diff "命令都使用 "Python diff"?