上一篇:02【Git的基本使用-快速上手Git】
下一篇:04【彻底掌握Git的底层对象】
目录:【Git系列教程-目录大纲】
文章目录
三、Git底层对象
Git 的核心部分是一个简单的键值对数据库。你可以向该数据库插入任意类型的内容,它会返回一个键值,通过该键值可以在任意时刻再次检索该内容,我们之前使用add、commit、rm、mv等命令时其实就是在向Git这个数据库读取/添加数据。
需要注意的是,只要往Git中添加了数据,那么数据就永远不会被删除,使用git删除命令只是进行了一次新的版本的迭代。学习Git的底层命令有助于我们对Git的底层原理有一个更加清晰的认识;
在Git中存在三个元数据对象,分别为Blob对象(文本对象)、Tree对象(树对象)、Commit对象(提交对象),当我们在使用Git命令往Git中写数据时,Git会将其封装为这三种对象,三种不同的对象封装了不同的数据;其存储位置在.git/objects
目录中;
3.1 Blob对象
3.1.1 Blob对象简介
Blob对象也称文本对象,当某个文本需要添加到Git中时,Git会将其包裹成一个Blob对象存储到Git数据库中,每个Blob对象都会有一个唯一的hash值,这就是Git中的最原始的版本信息。当修改文件后,需要再次将新的文件包裹成Blob对象存入Git数据库,这又是一次文件版本的更新;
3.1.2 Blob对象的使用
1)写入数据
- 语法:
git hash-object -w {文件名|目录名}
将数据写入Git:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui
$ git init # 初始化git仓库
Initialized empty Git repository in C:/Users/Admin/Desktop/workspace/xiaohui/.git/
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui (master)
$ echo "111" >> aaa.txt # 创建文件
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui (master)
$ git hash-object -w aaa.txt # 将aaa.txt文件写入Git
warning: LF will be replaced by CRLF in aaa.txt.
The file will have its original line endings in your working directory
58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
文件生成的路径默认在:.git\objects,其中生成的文件夹名称是hash值的前两位,其余的位数作为文件名
2)读取数据
- 语法:
git cat-file {-p|-t} {hash}
-p
:获取文件内容-t
:获取文件类型
从Git读取数据:
git cat-file -p 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # 获取Git对象的内容
git cat-file -t 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # 获取Git对象的类型
3)小练习
创建一个新的文件写入到Git中:
echo "Hello World" >> bbb.txt # 创建一个新的文件
git hash-object -w bbb.txt # 写入到Git中
查看objects目录:
读取文件内容:
git cat-file -p 557db03de997c86a4a028e1ebd3a1ceb225be238
修改文件内容,再次查看Git数据库中的数据:
echo "Hello Git" >> bbb.txt
git cat-file -p 557db03de997c86a4a028e1ebd3a1ceb225be238
重新将bbb.txt添加到Git数据库:
git hash-object -w bbb.txt # 将bbb.txt重新添加到git数据库
find .git/objects/ -type f # 查询objects目录下的所有文件
.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 # bbb.txt ---> HelloWorld
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt ---> 111
.git/objects/93/f515c1fffa123e2dc1ad3015fd59a421afacd2 # bbb.txt ---> Hello World Hello Git
查询Git数据库,重新生成了一个Blob对象
- 读取Blob对象:
git cat-file -p 557db03de997c86a4a028e1ebd3a1ceb225be238
git cat-file -p 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
git cat-file -p 93f515c1fffa123e2dc1ad3015fd59a421afacd2
3.2 Tree对象
3.2.1 Tree对象简介
Blob对象中只会存储文本内容而不知道该文本的文件名称。同时,生成Blob对象代表是文件的一次版本,但是有时我们需要将多个操作归纳为一个版本。因此,在Git中,Blob对象只是代表文件的一个版本,而不是代表项目的一个版本,因为一次项目的版本可能涉及到多个文件的操作。
Tree对象也称树对象,一个树对象包含了一条或多条记录,一个Tree对象就是一次操作的版本,Tree对象中保存有该对象所涉及到的所有文件(包括文件名称)。在Git中,一个Tree对象才能称得上是一次真正意义上的版本(快照);
另外,一个Tree对象还可以包含另一个Tree对象。
3.2.2 暂存区的概念
一个Tree对象是由一条或多条记录组成的,那么如何让Tree对象包含多个记录呢?
我们在生成Tree对象之前,需要将所有的记录都存储到"暂存区",暂存区用于"暂存"一些操作,等这些操作足够成为一次版本时就将暂存区中的内容生成一个Tree对象,这样Tree对象就包含了多个记录了。
- 将记录添加到暂存区:
git update-index --add --cacheinfo {文件模式} {hash} {文件|目录}
文件模式:
- 100644:普通文件
- 100755:可执行文件
- 120000:符号链接
- 查看暂存区:
git ls-files -s
3.3.2 生成Tree对象
【语法】
git write-tree # 将当前暂存区的内容生成Tree对象
【练习】
- 1)初始化git仓库:
rm -rf ./* .git
git init
- 2)生成第一个Blob对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "111" >> aaa.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git hash-object -w aaa.txt
warning: LF will be replaced by CRLF in aaa.txt.
The file will have its original line endings in your working directory
58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -t 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
blob
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
111
2)生成第二个Blob对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "222" >> bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git hash-object -w bbb.txt
warning: LF will be replaced by CRLF in bbb.txt.
The file will have its original line endings in your working directory
c200906efd24ec5e783bee7f23b5d7c941b0c12c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -t c200906efd24ec5e783bee7f23b5d7c941b0c12c
blob
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p c200906efd24ec5e783bee7f23b5d7c941b0c12c
222
3)将两个记录添加到暂存区:
git update-index --add --cacheinfo 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt
git update-index --add --cacheinfo 100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
4)查看暂存区:
git ls-files -s
5)将两次操作生成Tree对象(一次版本)
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 将暂存区的内容生成Tree对象
$ git write-tree
32dcf33783f09530a55367ae95a221b9ee1c1eba
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看objects目录中的文件
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看该hash对应的git类型
$ git cat-file -t 32dcf33783f09530a55367ae95a221b9ee1c1eba
tree
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看该hash的内容
$ git cat-file -p 32dcf33783f09530a55367ae95a221b9ee1c1eba
100644 blob 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
6)查看暂存区:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git ls-files -s
100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt
一个Tree对象中包含多个变更的Blob对象,Blob对象对应我们实际开发中的一个个操作,当一个个操作满足了一次版本的要求时,我们就会生成对应的Tree对象来生成版本。因此,我们在实际使用Git时,一个Tree对象往往才是代表一个项目的某个版本,而非Blob对象;
3.2.3 Tree对象小练习
1)生成第一个树对象
- 1)初始化Git仓库:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace
$ git init
Initialized empty Git repository in C:/Users/Admin/Desktop/workspace/.git/
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "111" >> aaa.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git hash-object -w aaa.txt # 生成Blob对象
warning: LF will be replaced by CRLF in aaa.txt.
The file will have its original line endings in your working directory
58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "222" >> bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git hash-object -w bbb.txt # 生成Blob对象
warning: LF will be replaced by CRLF in bbb.txt.
The file will have its original line endings in your working directory
c200906efd24ec5e783bee7f23b5d7c941b0c12c
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 添加到暂存区
$ git update-index --add --cacheinfo 100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 添加到暂存区
$ git update-index --add --cacheinfo 100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git ls-files -s # 查看暂存区的内容
100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git write-tree # 生成Tree对象
32dcf33783f09530a55367ae95a221b9ee1c1eba
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象(包含aaa.txt和bbb.txt)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt
查看树对象的内容:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 32dcf33783f09530a55367ae95a221b9ee1c1eba
100644 blob 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
2)生成第二个树对象
修改aaa.txt内容:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "1010" >> aaa.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git hash-object -w aaa.txt # 生成Blob对象
warning: LF will be replaced by CRLF in aaa.txt.
The file will have its original line endings in your working directory
7b481520925a2e75716034e3c858b7ef2a9aae75
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象(包含aaa.txt和bbb.txt)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)
将aaa.txt添加到暂存区:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git ls-files -s
100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git update-index --cacheinfo 100644 7b481520925a2e75716034e3c858b7ef2a9aae75 aaa.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git ls-files -s
100644 7b481520925a2e75716034e3c858b7ef2a9aae75 0 aaa.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt
Tips:aaa.txt文件已经添加了到了暂存区,第二次不需要再指定
--add
参数了;
将当前暂存区的内容生成Tree对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git write-tree
a24447346b4470013f38a67d14d97f975e39c037 # 本次树对象的hash值
查看所有Git对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象v1(包含aaa.v1和bbb)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/a2/4447346b4470013f38a67d14d97f975e39c037 # 树对象v2(包含aaa.v2和bbb)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)
查看第2个树对象的内容:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 32dcf33783f09530a55367ae95a221b9ee1c1eba
100644 blob 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt # v1版本
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p a24447346b4470013f38a67d14d97f975e39c037
100644 blob 7b481520925a2e75716034e3c858b7ef2a9aae75 aaa.txt # v2版本
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
3.2.4 读取树对象
【语法】
git read-tree --prefix=bak {Tree-hash} # 根据Tree对象的hash来读取树,将读取到的内容写入暂存区
【练习】
读取树对象v2的内容到暂存区:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git read-tree --prefix=bak a24447346b4470013f38a67d14d97f975e39c037 # 读取版本2的树对象(包含aaa.v2和bbb)到暂存区
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git ls-files -s
100644 7b481520925a2e75716034e3c858b7ef2a9aae75 0 aaa.txt # aaa.v2(原来暂存区就有的内容)
100644 7b481520925a2e75716034e3c858b7ef2a9aae75 0 bak/aaa.txt # 树对象.v2中的aaa
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bak/bbb.txt # 树对象.v2中的bbb
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt # bbb.v1(原来暂存区就有的内容)
将暂存区中的内容生成Tree对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git write-tree
bdf7b58c9bfca7f494b2575ffed44cde91f80ce5
查看所有Git对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象v1(包含aaa.v1和bbb)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/a2/4447346b4470013f38a67d14d97f975e39c037 # 树对象v2(包含aaa.v2和bbb)
.git/objects/bd/f7b58c9bfca7f494b2575ffed44cde91f80ce5 # 树对象v3(树对象v2和树对象v1)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)
查看三个树对象的内容:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 32dcf33783f09530a55367ae95a221b9ee1c1eba
100644 blob 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c aaa.txt # v1版本
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p a24447346b4470013f38a67d14d97f975e39c037
100644 blob 7b481520925a2e75716034e3c858b7ef2a9aae75 aaa.txt # v2版本
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p bdf7b58c9bfca7f494b2575ffed44cde91f80ce5
100644 blob 7b481520925a2e75716034e3c858b7ef2a9aae75 aaa.txt # v2版本
040000 tree a24447346b4470013f38a67d14d97f975e39c037 bak # 树对象v2版本
100644 blob c200906efd24ec5e783bee7f23b5d7c941b0c12c bbb.txt
此时树对象(v3)包含一个树对象(v2)和两个Blob对象;
3.3 Commit对象
3.3.1 Commit对象简介
Tree对象代表了项目中的一次版本快照,但是Tree对象缺失了一些日志信息,例如本次版本快照是谁(哪个开发人员)产生的?本次的版本快照的主要内容(日志信息)是什么?等等。
Commit对象也称提交对象,Commit对象是对Tree对象的包裹添加一些注释信息。值得注意的是:虽然Tree对象代表一次项目的版本快照,但是由于缺少注释信息,我们在生成Tree对象时还会将其打包为Commit对象,就这样,项目的一次完整版本快照就生成完毕了。
3.3.2 生成Commit对象
【语法】
echo '提交注释' | git commit-tree {tree-hash} # 将指定的tree对象包裹为commit对象
查询所有Git对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象v1(包含aaa.v1和bbb)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/a2/4447346b4470013f38a67d14d97f975e39c037 # 树对象v2(包含aaa.v2和bbb)
.git/objects/bd/f7b58c9bfca7f494b2575ffed44cde91f80ce5 # 树对象v3(树对象v2和树对象v1)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)
- 生成提交对象
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "提交tree.v1" | git commit-tree 32dcf33783f09530a55367ae95a221b9ee1c1eba # 生成提交对象
44811b2b63f5cbbc118012d7a8831479354f2c20
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -t 44811b2b63f5cbbc118012d7a8831479354f2c20 # 查看Git类型的类型
commit
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 44811b2b63f5cbbc118012d7a8831479354f2c20 # 查看提交对象的内容
tree 32dcf33783f09530a55367ae95a221b9ee1c1eba # 该提交对象所包裹的树对象
author xiaohui <xiaohui@aliyun.com> 1696939487 +0800 # 作者信息
committer xiaohui <xiaohui@aliyun.com> 1696939487 +0800
提交tree.v1 # 日志信息
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 查看暂存区(生成提交对象不会对暂存区有影响)
$ git ls-files -s
100644 7b481520925a2e75716034e3c858b7ef2a9aae75 0 aaa.txt
100644 7b481520925a2e75716034e3c858b7ef2a9aae75 0 bak/aaa.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bak/bbb.txt
100644 c200906efd24ec5e783bee7f23b5d7c941b0c12c 0 bbb.txt
Tips:生成Blob对象和Tree对象时,只要操作一致多次生成的Blob对象和Tree对象的编号是一样的,但是Commit对象的编号即使多次操作一致,编号也会不一样
查看所有Git对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象v1(包含aaa.v1和bbb)
.git/objects/44/811b2b63f5cbbc118012d7a8831479354f2c20 # 提交对象
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/a2/4447346b4470013f38a67d14d97f975e39c037 # 树对象v2(包含aaa.v2和bbb)
.git/objects/bd/f7b58c9bfca7f494b2575ffed44cde91f80ce5 # 树对象v3(树对象v2和树对象v1)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)
3.3.3 指定父Commit对象提交
在生成Commit对象对象时,我们可以设置该Commit对象的父Commit对象,代表本次的Commit对象是基于上一次Commit的版本更新;这样就形成了一个版本更新链路,我们根据这个链路就可以进行版本穿梭。
指定父对象生成提交对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ echo "提交tree.v2" | git commit-tree a24447346b4470013f38a67d14d97f975e39c037 -p 44811b2b63f5cbbc118012d7a8831479354f2c20
4d7779d1ef87bca1e988b76e4dd1cf9b7b47f858
查询提交对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -t 4d7779d1ef87bca1e988b76e4dd1cf9b7b47f858
commit
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ git cat-file -p 4d7779d1ef87bca1e988b76e4dd1cf9b7b47f858 # 查看提交对象内容
tree a24447346b4470013f38a67d14d97f975e39c037
parent 44811b2b63f5cbbc118012d7a8831479354f2c20 # 父提交对象
author xiaohui <xiaohui@aliyun.com> 1696941179 +0800 # 作者信息
committer xiaohui <xiaohui@aliyun.com> 1696941179 +0800
提交tree.v2 # 日志信息
查看所有的Git对象:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)
$ find .git/objects/ -type f
.git/objects/32/dcf33783f09530a55367ae95a221b9ee1c1eba # 树对象v1(包含aaa.v1和bbb)
.git/objects/44/811b2b63f5cbbc118012d7a8831479354f2c20 # 提交对象(树对象v1)
.git/objects/4d/7779d1ef87bca1e988b76e4dd1cf9b7b47f858 # 提交对象(树对象v2)
.git/objects/58/c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c # aaa.txt(版本1)
.git/objects/7b/481520925a2e75716034e3c858b7ef2a9aae75 # aaa.txt(版本2)
.git/objects/a2/4447346b4470013f38a67d14d97f975e39c037 # 树对象v2(包含aaa.v2和bbb)
.git/objects/bd/f7b58c9bfca7f494b2575ffed44cde91f80ce5 # 树对象v3(树对象v2和树对象v1)
.git/objects/c2/00906efd24ec5e783bee7f23b5d7c941b0c12c # bbb.txt(版本1)