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,最终形成提交的历史记录。

相关推荐
李少兄16 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
先跑起来再说1 天前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
承渊政道1 天前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力1 天前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
摇滚侠1 天前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东1 天前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应2 天前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应2 天前
Git本地仓库命令补充
git
sun0077002 天前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎12 天前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份