git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

时间:2024-04-14 15:43:59

1.git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(a)新建一个文件夹“gitDiffTest”,在里面新建2个txt文档,并写入内容(具体见以下图片)

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(b)通过GitBash使用git init,git add -A,git commit –m “First Commit”命令第一次提交。

该操作用于生成第一份commit提交过的版本,用于待会使用git diff等命令用于对比

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(c)现在我们修改(a)中的2个txt文档,并添加一个新的txt文档(具体见以下图片)

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(d)使用git add命令把added_uncommitted.txt导入暂存区,再用git status命令查看目前情况

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

在下面的大图片中,我们通过git status看到了有3大分类:“Changes to be committed”,

“Changes not staged for committed”,“Untracked files”,每大分类里面分别有一个1文件,

我们将通过git diff等相似命令,我们便可以体验到不同“git diff等相似命令”对这3大分类

文件用不同的操作和对比。

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(e)使用git diff命令,发现Git只对比了“tracked_unadded.txt”文件

git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

(f)使用命令git diff –cached, 发现Git只对比了“added_uncommitted.txt”文件

    git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

    (g)使用命令git diff HEAD,发现Git对比了“tracked_unadded.txt”文件

和“added_uncommitted.txt”文件

    git diff 和 git diff –cached 和 git diff HEAD实际应用中的区别对比+详细解释

    (h)总结:

  1. git diff: 当工作区有改动,临时区为空,diff的对比是“工作区最后一次commit提交的仓库的共同文件”;当工作区有改动,临时区不为空,diff对比的是“工作区暂存区的共同文件”。
  2. git diff --cached 或 git diff --staged:显示暂存区(已add但未commit文件)和最后一次commit(HEAD)之间的所有不相同文件的增删改
  3. git diff HEAD:显示工作目录(已track但未add文件)和暂存区(已add但未commit文件)与最后一次commit之间的的所有不相同文件的增删改

git diff基础文章:git diff的最全最详细的4大主流用法

git diff进阶文章2:git diff的对比对象的争议?实际代码测试+详细解释

 

本文参考来源:

[1] https://blog.****.net/clxjoseph/article/details/80213315

[2] https://www.cnblogs.com/oxspirt/p/5970101.html

[3] https://blog.****.net/AsheAndWine/article/details/78982919

[4] https://blog.****.net/whbing1471/article/details/52065448

[5] https://www.jianshu.com/p/bb97fabb475e

[6] https://blog.****.net/qq_37281252/article/details/79064638