本文通过实例,分析Git中的commit、tree和 blob。
新建Git仓库。
$ git init
创建文件夹images。
$ mkdir images
在文件夹images中新建文件read.md,写入内容“hello,git”。
$ echo "hello,git" > images/read.md
提交到暂存区。
$ git add images
提交到仓库。
$ git commit -m "add readme.md"
查看Git中的对象。
$ find .git/objects -type f
.git/objects/43/09cccf7c051ee6307bc3bbf124b1dbaf913850
.git/objects/44/45bcd8d5524b5168e23e275c2f4db6119ed921
.git/objects/17/60e342016dc15d8840fd67ec4520b108c3275f
.git/objects/d4/9b94a53d30cafa4cd556db3dc2f3dd6851a87b
分析对象的类型和内容。
$ git cat-file -t 4309ccc
blob
$ git cat-file -p 4309ccc
hello,git
由此可知:“4309ccc”是blob对象,内容为“hello,git”。
用同样的方法分析其它三个对象,结果如下:
对象 | 类型 | 内容 |
---|---|---|
4309ccc | blob | hello,git |
4445bcd | tree | 040000 tree d49b94a53d30cafa4cd556db3dc2f3dd6851a87b images |
1760e34 | commit | tree 4445bcd8d5524b5168e23e275c2f4db6119ed921 author April <***@163.com> 1555819583 +0800 committer April <***@163.com> 1555819583 +0800add readme.md |
d49b94a | tree | 100644 blob 4309cccf7c051ee6307bc3bbf124b1dbaf913850 read.md |
根据以上实例,可以总结出Git中commit、tree和 blob关系如下: