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

Git 原理(提交对象)

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

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

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

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

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

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

我们可以自己手动创建个文件夹,然后里面创建个 index.html,并通过 git init 进行初始化。

接下来,进入目录下的隐藏文件夹 .git,所有需要存储的东西都会存储在 git 下的 objects 中:

bash 复制代码
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 文件夹,里面存放二进制文件。

bash 复制代码
nathanchen@192 objects % ls
69   info pack

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

bash 复制代码
git cat-file -t file # 查看文件的种类
git cat-file -p file # 查看文件的内容
bash 复制代码
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 两份文件

bash 复制代码
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
bash 复制代码
nathanchen@192 objects % ls
0a   69   8d   info pack

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

bash 复制代码
nathanchen@192 0a % git cat-file -t 0a68    
tree
bash 复制代码
nathanchen@192 0a % git cat-file -p 0a68
100644 blob 69763a710c37f7ec0e610fb4ae1cbde7e8e7524d    index.html
bash 复制代码
nathanchen@192 8d % git cat-file -t 8d81
commit
bash 复制代码
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 下新增的文件

bash 复制代码
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,它会指向另一个对象,这个对象包含本次提交中关联对象的映射关系,从而找到在暂存区中的二进制文件。

结合以上案例进行理解:

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

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

相关推荐
悟空20164 小时前
001、Git开发流程规范
git
Li小李同学Li4 小时前
git学习【持续更新中。。。】
git·学习·elasticsearch
晨春计6 小时前
【git】
android·linux·git
念幽6 小时前
Git常用命令
git
神技圈子7 小时前
【git系列】git中的那些迷惑的术语以及概念详解
git
benben0447 小时前
Photoshop使用方法大全
git
ou.cs8 小时前
git 删除远程分支的几种写法
git
atlanteep8 小时前
Linux·权限与工具-git与gdb
linux·git
胆小鬼~18 小时前
【DAY20240918】03教你轻松配置 Git 远程仓库并高效推送代码!
git
哆啦安全21 小时前
git常用命令(patch补丁和解决冲突)
git