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 <[email protected]> 1726479021 +0800
committer Captain Drake <[email protected]> 1726479021 +0800

1 commit

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

bash 复制代码
nathanchen@NathansMacBook-Pro test3 % git log 
commit 8d8182a27fe263a6c20a8136c1dbc7768f988e02 (HEAD -> master)
Author: Captain Drake <[email protected]>
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,最终形成提交的历史记录。

相关推荐
bubiyoushang8886 小时前
解决 Git 访问 GitHub 时的 SSL 错误
git·github·ssl
海码00711 小时前
【版本控制】Git 和 GitHub 入门教程
git·github
网硕互联的小客服14 小时前
503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
服务器·git·github
abcnull15 小时前
github开源协议选择
git·github·开源协议
安庆平.Я18 小时前
git互联GitHub 使用教程
git·github
自来也_1 天前
Git配置代理
git
Jooolin2 天前
【编程史】Git是如何诞生的?这可并非计划之中...
linux·git·ai编程
Lw老王要学习2 天前
VScode 使用 git 提交数据到指定库的完整指南
windows·git·vscode
去旅行、在路上2 天前
Git & Svn
git·svn
abcnull2 天前
github中main与master,master无法合并到main
git·github