目录
文章目录
实验环境
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
也可以用浏览器看下效果:
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 }
修完完成保存退出,验证效果:
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、缓存区、工作区之间的文件内容
假定:HEAD、缓存区、工作区中的readme.md文件内容均不相同。
git diff HEAD -- readme.md # 工作区 <===> HEAD
git diff -- readme.md # 工作区 <===> 缓存区
git diff --cached -- readme.md # 缓存区 <===> HEAD
1.2.3.4注意:–的作用
-- 为了让git命令读取命令参数的时候消除歧义用的,双连字符后面的是路径或文件
1.2.3.5注意:本地仓库的概念
作者回复: 我们一般称本地仓库,除了工作区外还包括.git目录,所以不是同一个东西。
关于我
我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!
各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:
-
个人微信二维码:x2675263825 (舍得), qq:2675263825。
-
个人博客地址:www.onlyonexl.cn
-
个人微信公众号:云原生架构师实战
-
个人csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
最后
好了,关于怎么比较工作区和暂存区所含文件的差异?实验就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!