实战:怎么比较工作区和暂存区所含文件的差异?-2021.11.26

image-20211126112000445

目录

实验环境

win10
$ git version
git version 2.17.0.windows.1

实验软件(无)

1 实战演示

1.1 问题需求:怎么比较工作区和暂存区所含文件的差异?

1.2 实验步骤

1.2.1 查看当前实验环境
#可以看到当前工作区是clean状态的
hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ gs
On branch master
nothing to commit, working tree clean 

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)

#可以看到当前master分支有3次commit
$ git log --graph
* commit 7b84bebf129ffd961c060e7bd6a5f9ce91ac5f3d (HEAD -> master)
| Author: xyy <xyy@189.cn>
| Date:   2021-11-19 06:52:42 +0800
|
|     Add the first command with config
|
* commit 37affa8101e424ef7cedb4738ee669fe2fb0b9fd
| Author: xyy <xyy@189.cn>
| Date:   2021-10-05 07:12:54 +0800
|
|     Create a complete web page!
|
|     Add index + git logo
|
|     Add style.css
|
|     Add js
|
|     Add a refering project !
|
* commit 70c905452ef82796055a52ccf47612038f5b2e01
  Author: xyy <xyy@189.cn>
  Date:   2021-10-04 08:13:30 +0800

      Add readme

      Add readme.md

      Move  filename readme.md readme

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$
1.2.2 来模拟下今天的测试环境

step1.首先对index.html文件修改下(添加下我们之前学习的知识点bare repostrity概念),再然后提交到缓存区

1.编辑index.html文件
hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ vim index.html
……
 13     <section>
 14         <div class="accordion"><h1>Terminologys</h1></div>
 15             <div class="panel">
 16                 <ol>
 17                     <li>basre repostrity</li> #只在第17行这个里面添加下bare repostrity就好
 18                     <li></li>
 19                     <li></li>
 20                     <li></li>
 ……
 编辑完成后保存退出
 
 2.将编辑好的index.html文件提交到暂存区
 hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ git add index.html

也可以用浏览器看下效果:

image-20211126065611499

step2.再修改下stycle.css内容(修改网站字体为黑色字体),修改完后你想出去喝杯茶了

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ vim styles/style.css
  1 body{
  2   background-color: orange;
  3   font-family: 'Monaco', sans-serif;
  4   color: black; #将这里的white修改为black
  5 }
修完完成保存退出,验证效果:

image-20211126070402728

step3.咦,你喝完茶回来后,想看下当前工作区和暂存区的内容有哪些不同,此时,该用什么命令呢?

1.我们发现通过gs命令也可以看到工作区和缓存区哪个文件有发生变化,但至于具体哪里发生了变化,就需要用git diff 命令来查看了
hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ gs
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   styles/style.css


hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)

2.git diff命令默认就是用来比较工作区和暂存区所含文件的差异
$ git diff
diff --git a/styles/style.css b/styles/style.css
index b72e61c..7479a7c 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
 body{
   background-color: orange;
   font-family: 'Monaco', sans-serif;
-  color: white; #这个指的是暂存区的内容
+  color: black; #这个指的是工作区的内容
 }

 body a{

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)

step4.现在,我们不妨做做几次修改,再来观察下这个git diff命令的效果

本次我们来修改下readme文件:

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ vim readme
  1 # say hello #添加此行内容
  2 i love you,xyy!
  保存退出

此时,我们在使用git diff命令来观察下效果:

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ git diff
diff --git a/readme b/readme
index 58999ec..0f3e8e3 100644
--- a/readme
+++ b/readme
@@ -1 +1,2 @@
+# say hello  #可以看到这里发生了改变
 i love you,xyy!
diff --git a/styles/style.css b/styles/style.css
index b72e61c..7479a7c 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
 body{
   background-color: orange;
   font-family: 'Monaco', sans-serif;
-  color: white;
+  color: black; #可以看到这里发生了改变
 }

 body a{

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$

step5.注意:

默认情况git diff命令会把工作区和暂存区所有的文件进行对比,如果你想对指定文件进行比较,可使用如下命令:

语法:
git diff -- 指定文件 #注意:这里可以是一个文件,也可以是多个文件

1.git diff -- 一个文件
hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ git diff -- readme
diff --git a/readme b/readme
index 58999ec..0f3e8e3 100644
--- a/readme
+++ b/readme
@@ -1 +1,2 @@
+# say hello
 i love you,xyy!

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)

$ git diff -- styles/style.css
diff --git a/styles/style.css b/styles/style.css
index b72e61c..7479a7c 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
 body{
   background-color: orange;
   font-family: 'Monaco', sans-serif;
-  color: white;
+  color: black;
 }

 body a{

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$

2.git diff -- 多个文件
hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$ git diff -- readme styles/style.css
diff --git a/readme b/readme
index 58999ec..0f3e8e3 100644
--- a/readme
+++ b/readme
@@ -1 +1,2 @@
+# say hello
 i love you,xyy!
diff --git a/styles/style.css b/styles/style.css
index b72e61c..7479a7c 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
 body{
   background-color: orange;
   font-family: 'Monaco', sans-serif;
-  color: white;
+  color: black;
 }

 body a{

hg@LAPTOP-G8TUFE0T MINGW64 /d/IT/01 IT/github_repo/git_repository/git_learning (master)
$

好了,实验到此结束。

1.2.3 命令总结

1.2.3.1 怎么比较工作区和暂存区所含文件的差异?
1.git diff #默认情况`git diff`命令会把工作区和暂存区所有的文件进行对比
2.git diff 一个文件
3.git diff 多个文件
1.2.3.2 如何比较HEAD、缓存区、工作区之间的文件内容

image-20211126104609328

假定:HEAD、缓存区、工作区中的readme.md文件内容均不相同。
git diff HEAD -- readme.md # 工作区 <===> HEAD
git diff -- readme.md # 工作区 <===> 缓存区
git diff --cached -- readme.md # 缓存区 <===> HEAD
1.2.3.4注意:–的作用

image-20211126105206311

-- 为了让git命令读取命令参数的时候消除歧义用的,双连字符后面的是路径或文件
1.2.3.5注意:本地仓库的概念

image-20211126105330033

作者回复: 我们一般称本地仓库,除了工作区外还包括.git目录,所以不是同一个东西。

关于我

我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!

各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:

  1. 个人微信二维码:x2675263825 (舍得), qq:2675263825。

    image-20211002091450217

  2. 个人博客地址:www.onlyonexl.cn

    image-20211002092057988

  3. 个人微信公众号:云原生架构师实战

    image-20211002141739664

  4. 个人csdn

    https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

    image-20211002092344616

最后

​ 好了,关于怎么比较工作区和暂存区所含文件的差异?实验就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值