内容寻址+Git三种对象

本文可理解为对官方文档的概括和自我理解

https://git-scm.com/book/zh/v1/Git-%E5%86%85%E9%83%A8%E5%8E%9F%E7%90%86-Git-%E5%AF%B9%E8%B1%A1

基于内容寻址

我们熟悉的windows系统是基于文件名的寻址系统。我们可根据文件进行定位和查找。而GIT,是将文件的内容计算得到一个hash值,而这个hash值是该内容的唯一索引。一个这种键值对就是一个blob对象

# 查看'test content'在git中的hash值
[wlin@wlin local_version]$ echo 'test content' | git hash-object --stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4
# 查看含有'test content'不同文件在git中hash值 
[wlin@wlin local_version]$ echo 'test content' > file
[wlin@wlin local_version]$ git hash-object file
d670460b4b4aece5915caf5c68d12f560a9fe3e4
[wlin@wlin local_version]$ echo 'test content' > file_new
[wlin@wlin local_version]$ git hash-object file_new
d670460b4b4aece5915caf5c68d12f560a9fe3e4
# 以上可见,只要内容相同,hash值总是一样的
# 现在让我们存储这些内容。
[wlin@wlin local_version]$ !find
find .git/objects -type f
[wlin@wlin local_version]$ git hash-object -w file
d670460b4b4aece5915caf5c68d12f560a9fe3e4
[wlin@wlin local_version]$ find .git/objects -type f
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
[wlin@wlin local_version]$ git hash-object -w file_new
d670460b4b4aece5915caf5c68d12f560a9fe3e4
[wlin@wlin local_version]$ find .git/objects -type f
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
# 以上可见,存储的对象也是只有一个,即一个hash值索引对应一个数据文件。
# 且这个hash值的前两位为object的子目录,后面的14位为文件名。
# 现在让我们基于这个内容的索引获取其内容
[wlin@wlin local_version]$ git cat-file -p d670460b4b4aece5915caf5c68d12f560a9fe3e4
test content

Git的三个对像

blob, tree, commit为git的三种对象。让我们生成三种对象。

[wlin@wlin local_version]$ touch test.txt
[wlin@wlin local_version]$ echo "version 1" > test.txt 
[wlin@wlin local_version]$ git init
Initialized empty Git repository in /home/wlin/local_version/.git/
[wlin@wlin local_version]$ ll .git/
branches/    config       description  HEAD         hooks/       info/        objects/     refs/
# 当前没有任何对象        
[wlin@wlin local_version]$ ll .git/objects/
info/ pack/ 
# 为文件创建hash值
[wlin@wlin local_version]$ git hash-object -w test.txt
83baae61804e65cc73a7201a7252750c76066a30
# 生成blob对象
[wlin@wlin local_version]$ ll .git/objects/
total 12
drwxrwxr-x. 2 wlin wlin 4096 Jan 23 10:24 83
drwxrwxr-x. 2 wlin wlin 4096 Jan 23 10:24 info
drwxrwxr-x. 2 wlin wlin 4096 Jan 23 10:24 pack
[wlin@wlin local_version]$ git cat-file -p 83baae61804e65cc73a7201a7252750c76066a30
version 1
[wlin@wlin local_version]$ git cat-file -t 83baae61804e65cc73a7201a7252750c76066a30
blob
# 并没有index目录
[wlin@wlin local_version]$ ll .git/
branches/    config       description  HEAD         hooks/       info/        objects/     refs/        
# 可以看到git检测到有尚未跟踪的文件
[wlin@wlin local_version]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	test.txt
nothing added to commit but untracked files present (use "git add" to track)
# 将blob对象放入存储区并生成index
[wlin@wlin local_version]$ git update-index --add --cacheinfo 100644 83baae61804e65cc73a7201a7252750c76066a30 test.txt
# 可以看到index已经生成
[wlin@wlin local_version]$ ll .git/
branches/    config       description  HEAD         hooks/       index        info/        objects/     refs/        
# 写入tree(Git根据暂存区域或者index来创建并写入tree对象)
[wlin@wlin local_version]$ git write-tree
d8329fc1cc938780ffdd9f94e0d364e0ea74f579
# 我们将一个对象放入了index则可以看到该tree显示了一个对象
[wlin@wlin local_version]$ git cat-file -p d8329fc1cc938780ffdd9f94e0d364e0ea74f579
100644 blob 83baae61804e65cc73a7201a7252750c76066a30	test.txt
# tree已经生成
[wlin@wlin local_version]$ git cat-file -t d8329fc1cc938780ffdd9f94e0d364e0ea74f579
tree
# 这个时候显示文件在暂存区
[wlin@wlin local_version]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   test.txt
#
# 生成commit对象
[wlin@wlin local_version]$ git write-tree
577e73a60a5e0b5ee1019a482606a9cd2e2e3168
[wlin@wlin local_version]$ echo "first-commit" | git commit-tree 
3b816fcbbd821fd74af82d177f01cd278f82cb85
[wlin@wlin local_version]$ git cat-file -p 3b816fcbbd821fd74af82d177f01cd278f82cb85
tree 577e73a60a5e0b5ee1019a482606a9cd2e2e3168
author Cara Wang <[email protected]> 1548243816 +0100
committer Cara Wang <[email protected]> 1548243816 +0100

first-commit
[wlin@wlin local_version]$ git cat-file -t 3b816fcbbd821fd74af82d177f01cd278f82cb85
commit

 

猜你喜欢

转载自blog.csdn.net/solinger/article/details/86603495