Git 原理(提交对象)(结合图与案例)

Git 原理(提交对象)

这一块主要讲述下 Git 的原理。

在进行提交操作时,Git 会保存一个提交对象(commit object):

该提交对象会包含一个指向暂存内容快照的指针;

该提交对象还包含了作者的姓名和邮箱、提交时输入的信息以及指向它的父对象的指针;

  • 首次提交产生的提交对象没有父对象,普通提交操作产生的提交对象有一个父对象;
  • 而由多个分支合并产生的提交对象有多个父对象;

接下来,通过实践的方式来探索 Git 提交 的原理。

我们可以自己手动创建个文件夹,然后里面创建个 index.html,并通过 git init 进行初始化。
接下来,进入目录下的隐藏文件夹 .git,所有需要存储的东西都会存储在 git 下的 objects 中:

nathanchen@192 tests2 % ls
index.html
nathanchen@192 tests2 % cd .git
nathanchen@192 .git % ls     
HEAD        config      description hooks       info        objects     refs

首先,我们修改一波 index.html 文件,然后进行git add .,这样会把所有文件放入到暂缓区中,再次查看对应文件,会发现多了 69 文件夹,里面存放二进制文件。

nathanchen@192 objects % ls
69   info pack

接下来查看文件的种类和对应内容,命令如下所示:

git cat-file -t file # 查看文件的种类
git cat-file -p file # 查看文件的内容
nathanchen@NathansMacBook-Pro 05 % pwd
/test3/.git/objects/69
nathanchen@192 69 % ls
763a710c37f7ec0e610fb4ae1cbde7e8e7524d
nathanchen@192 69 % git cat-file -t 6976
blob
nathanchen@192 69 % git cat-file -p 6976
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
</html>%

注:这里的 69 指的是 69 文件夹,而 76 指的69文件夹下的 76 开头的文件。

然后,我们进行一波提交,并在对应文件夹下查看,发现新增了 0a 和 8d 两份文件

nathanchen@NathansMacBook-Pro test3 % git commit -m "1 commit"
[master (root-commit) 8d8182a] 1 commit
 1 file changed, 11 insertions(+)
 create mode 100644 index.html
nathanchen@192 objects % ls
0a   69   8d   info pack

查看 0a 和 8d 两个文件夹及其对应文件,发现 0a 是包含文件依赖关系的对象,8d 为 Commit 对象

nathanchen@192 0a % git cat-file -t 0a68    
tree
nathanchen@192 0a % git cat-file -p 0a68
100644 blob 69763a710c37f7ec0e610fb4ae1cbde7e8e7524d    index.html
nathanchen@192 8d % git cat-file -t 8d81
commit
nathanchen@192 objects % git cat-file -p 8d81
tree 0a683a4b7535845bb37e7444ab95404933d547e5
author Captain Drake <9393079+captain_drake@user.noreply.gitee.com> 1726479021 +0800
committer Captain Drake <9393079+captain_drake@user.noreply.gitee.com> 1726479021 +0800

1 commit

这时候,我们再次通过 log 来进行查看提交历史,会发现 8d81… 正好对应 objects 下新增的文件


nathanchen@NathansMacBook-Pro test3 % git log 
commit 8d8182a27fe263a6c20a8136c1dbc7768f988e02 (HEAD -> master)
Author: Captain Drake <9393079+captain_drake@user.noreply.gitee.com>
Date:   Mon Sep 16 17:30:21 2024 +0800

    1 commit
(END)

总结:

1-关于 git add 和 git commit 的底层操作

git add会将对应文件转化为二进制文件保存在 objects 里面。

git commit 会输出一个 Commit 对象,里面包含属性tree,它会指向另一个对象,这个对象包含本次提交中关联对象的映射关系,从而找到在暂存区中的二进制文件。

image.png

结合以上案例进行理解:

image.png

2-关于每次 Commit 形成的链条

每次 Commit 都会输出 Commit对象,里面包含 tree 的相关信息。除了第一次的 Commit对象,之后的 Commit 对象都会包含 parent 属性,从而指向前一次 Commit,最终形成提交的历史记录。

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值