Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

02-commits #2

Open
whg4 opened this issue Jun 25, 2023 · 0 comments
Open

02-commits #2

whg4 opened this issue Jun 25, 2023 · 0 comments
Labels

Comments

@whg4
Copy link
Owner

whg4 commented Jun 25, 2023

2-Commits

2.1什么是Commit?

Commit对象

一个commit指向:

  • 一个tree

同时它还包含metadata:

  • 作者和提交者
  • 日期
  • message信息
  • 父commit(一个或多个)

commit的SHA1值是根据它的全部信息生成的。

Commit表格形式示例

commit

Commit指向父Commits和Trees

commit_g

2.2一个Commit是代码的快照

提交一个commit

初始化Git仓库,在当前目录添加一个hello.txt文件,提交一个commit,具体过程如下

make_acommit

通过tree .git/objects命令,我们可以发现多出了三个hash值。

look_in_objects

查看hash-object的内容

通过cat 文件名查看某个hash-object的内容,我们发现其内容是压缩后的数据。

cat_object

为了查看hash-object的信息,Git还提供了我们两个命令来查看hash-object的信息,命令如下

git cat-file -t hash值 # -t 表示打印hash-object的类型
git cat-file -p hash值 # -p 标志打印hash-object的内容

分别查看三个hash-object的信息,得到以下结果

Blob对象

blob_object

Tree对象

tree_object

Commit对象

commit_object

通过查看hash-object的数据,验证了Git的Blob、Tree、Commit对象的结构。

为什么我们不能改变Commits?

如果你改变了当前commit的任何数据,当前commit会有一个新的SHA1值。由commit对象的构成元素可知,即使文件没有发生变化,但是commit的创建时间会发生变化。

2.3引用

引用Commits的指针

有三个指针指向引用着commits:

  • Tags
  • Branches
  • HEAD - 一个指向当前commit的指针

pointer_to_commit

引用-底层细节

通过tree .git查看 .git 目录的结构,如下图所示

re_stru

refs/heads目录保存了branches指针。

使用git log --oneline查看当前commit的hash值,然后使用cat .git/refs/heads/master查看master指针的值,可以发现master指针指向了Initial commit

branch_point

最后,通过cat .git/HEAD命令读取HEAD的值,发现HEAD指向了当前的master分支

head_pointer

@whg4 whg4 added the git label Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant