打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Git 索引文件(index file)

《看日记学git》之十二


===

这次重点讲解索引文件(index file)的作用。

我们在提交工作时,使用最多的命令就是git commit -a了,但是这个将提交你所做的所有工作。其实,如果你了解commit的工作机制,你会知道我们可以自定义提交哪些部分到哪些工作树中,其实自由度很大的。

1

还记得之前我们建立的test-project工作目录么。我们继续在这个目录下演示讲解。

[rocrocket@wupengchong test-project]$ echo “hello world,again”>>file.txt

这次,我们不急着执行commit命令,而是先用git diff看看差别情况:

[rocrocket@wupengchong test-project]$ git diff
diff –git a/file.txt b/file.txt
index 60be00d..a559610 100644
— a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
Hi,rocrocket!
+hello world,again
好了,我们可以看到git报告了我们刚才所做的修改。下面我们来add一下,然后再git diff,看看diff有什么变化呢:

[rocrocket@wupengchong test-project]$ git add file.txt
[rocrocket@wupengchong test-project]$ git diff
[rocrocket@wupengchong test-project]$
大家可以看到在add之后的git diff的输出竟然为空了,但是此时我们尚未执行commit阿。如果这个时候你执行git diff HEAD,你仍然会看到修改报告:

[rocrocket@wupengchong test-project]$ git diff HEAD
diff –git a/file.txt b/file.txt
index 60be00d..a559610 100644
— a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
Hi,rocrocket!
+hello world,again

这就说明了一个问题:git diff不是在和HEAD比,而是另外一个“神秘”内容在比,而这个“神秘”内容就是“索引文件”!

索引文件(index file)就是.git/index文件,它是二进制形式的文件。我们可以用ls-files来检查它的内容。

[rocrocket@wupengchong test-project]$ git ls-files –stage  //一定要记住,此命令是用于查看index file的!!
100644 a55961026a22bdd4e938dcc90a4a83823a81f776 0       file.txt
[rocrocket@wupengchong test-project]$ git cat-file -t a5596
blob
[rocrocket@wupengchong test-project]$ git cat-file blob a5596
Hi,rocrocket!
hello world,again

狠明显,我们可以看到其内容已经是改进后的代码了,怪不得git-diff会输出空呢!

我们的结论就是git add的作用就是创建一个blob文件来记录最新的修改代码,并且在index file里添加一个到此blob的链接。

2

如果在git-diff后面加上参数HEAD,则git-diff会显示当前工作目录和最近一次提交之间的代码区别。

[rocrocket@wupengchong test-project]$ echo ‘again?’>>file.txt
[rocrocket@wupengchong test-project]$ git diff HEAD
diff –git a/file.txt b/file.txt
index 60be00d..dfb67dc 100644
— a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
Hi,rocrocket!
+hello world,again
+again?

如果使用参数–cached,则会比较index file和最近一次提交之间的代码区别。

[rocrocket@wupengchong test-project]$ git diff –cached
diff –git a/file.txt b/file.txt
index 60be00d..a559610 100644
— a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
Hi,rocrocket!
+hello world,again

按我的认识,更清楚且通俗的解释就是:git维护的代码分成三部分,“当前工作目录”<->“index file”<->git仓库。git commit会将index file中的改变写到git仓库;git add会将“当前工作目录”的改变写到“index file”;“commit -a”则会直接将“当前工作目录”的改动同时写到“index file”和“git仓库”。而git diff总会拿git仓库来作为比较对象之一。如果git diff的参数是HEAD,则另一个比较对象就确定为“当前工作目录”;如果参数是–cached,则另一个比较对象就被确定为“index file”。

由此可见,commit(不加-a选项)的作用是使用index来建立commit,而和当前目录无关。

3

status命令会显示当前状态的一个简单总结:

[rocrocket@wupengchong test-project]$ git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#    modified:   file.txt
#
# Changed but not updated:
#   (use “git add <file>…” to update what will be committed)
#
#    modified:   file.txt
#
上面两行黑体字中的Changes to be committed表示在index和commit的区别状况。而Changed but not updated表示当前目录和index的区别状况。

ps:下一步我们将学习Everyday Git.

over~

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
git学习小计(一)
情景分析“C语言的const关键字” | Linux大棚
我的Git笔记(二)
保存参数到一个文件中
《用chsh选择shell》-linux命令五分钟系列之十二
CentOS7搭建git服务器 – 飞嗨
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服