git原理探索实验1——git的三种对象

背景知识

git的三个区域

  • working directory
    • 也就是你所操作的那些文件
  • history
    • 你所提交的所有记录,文件历史内容等等。git是个分布式版本管理系统,在你本地有项目的所有历史提交记录;文件历史记录;提交日志等等
  • stage(index)
    • 暂存区域,本质上是个文件,也就是.git/index

git的三类对象

  • blob
    • 用于表示一个文件
  • tree
    • 用于表示一个目录
  • commit
    • 用于表示一个提交

实验过程

$ git init test
Initialized empty Git repository in C:/Users/MilesGO/Desktop/git/test/.git/
$ cd test/.git/objects
$ ls -la
total 4
drwxr-xr-x 1 MilesGO 197609 0 6月   9 14:29 ./
drwxr-xr-x 1 MilesGO 197609 0 6月   9 14:29 ../
drwxr-xr-x 1 MilesGO 197609 0 6月   9 14:29 info/
drwxr-xr-x 1 MilesGO 197609 0 6月   9 14:29 pack/

此时该目录下有info/和pack/目录,但都是空的
回到本仓库根目录,添加README文件,内容"hello git"
在查看objects目录,仍旧只包含两个空文件夹
说明工作区的文件变化,与objects目录无关

git add README

此时objects目录下,出现了8d文件夹,内有0e41234f24b6da002d962a26c2495ea16a425f文件,文件夹名和文件名拼起来刚好40位
info/和pack/目录仍然是空的
git cat-file -t 哈希值,这个指令可以看出一个git对象的类型,所以这个文件是一个blob对象

$ git cat-file -t 8d0e41
blob

git提供了git cat-file -p指令,可以查看git对象的内容

$ git cat-file -p 8d0e41
hello git

回到本仓库根目录

$ git commit -m 'add README'
[master (root-commit) 7ed2426] add README
 1 file changed, 1 insertion(+)
 create mode 100644 README

再次查看objects/目录,发现多出了两个对象
分别为28324fe5bad6e397de7a37aa7d8c94b6bf176b83和c489ab8a46d7563cba89cbe0ea0d0a7fa61ecbcb

$ git cat-file -t 28324fe5bad6e397de7a37aa7d8c94b6bf176b83
tree
$ git cat-file -t c489ab8a46d7563cba89cbe0ea0d0a7fa61ecbcb
commit

这两个文件分别对应了tree对象和commit对象
接下来修改README文件

echo 'hello git again' >> README

git ls-files --stage,这个指令可以查看暂存区的文件

$ git ls-files --stage
100644 f83422c24917dcb2c2f781c08d83ac2fbc363dd2 0       README
$ cd .git/objects/f8
$ ls
3422c24917dcb2c2f781c08d83ac2fbc363dd2
$ git cat-file -t f83422
blob
$ git cat-file -p f83422
hello git
hello git again

这个blob对象对应了刚刚修改后的README文件
到此为止,一共有1个commit对象,1个tree对象,2个blob对象,分别对应两个版本的README
接下来提交README的修改

$ git commit -m 'modify README'
[master 42a4acb] modify README
 1 file changed, 1 insertion(+)

$ git cat-file -t 42a4ac
commit
$ git cat-file -t b9641c
tree

又分别多出来1个commit对象和1个tree对象

$ git cat-file -p 42a4ac
tree b9641c1bd3e89e43cfaebf63305fc0a655635b50
parent c489ab8a46d7563cba89cbe0ea0d0a7fa61ecbcb
author MilesGO <[email protected]> 1560063566 +0800
committer MilesGO <[email protected]> 1560063566 +0800

modify README

$ git cat-file -p b9641c
100644 blob f83422c24917dcb2c2f781c08d83ac2fbc363dd2    README

42a4ac是一个commit对象,其对应的根目录是tree b9641c,其parent commit是c489ab
经过了这么多操作之后,info和pack目录还都是空的

猜你喜欢

转载自www.cnblogs.com/milesgo517/p/10993618.html