git存储blob对象在哪里?

时间:2015-02-09 17:49:21

标签: git

我理解git压缩文件,然后计算SHA1并将其存储在.git/objects/中,我们可以使用git cat-file -p 'sha1'查看内容但我有兴趣知道git在哪里存储压缩的blob对象。

如以下帖子所述

http://gitready.com/beginner/2009/02/17/how-git-stores-your-data.html

更新 我只能在SHA1中看到.git/objects,我认为它是指实际blob而不是blob

1 个答案:

答案 0 :(得分:5)

  

我理解git压缩文件然后计算SHA1

Git计算未压缩文件的SHA1哈希值,然后压缩数据并将其存储在.git / objects中,使用SHA1作为文件名。

示例:

1。创建一个测试库

$ mkdir tmp
$ cd tmp
$ git init .
Initialized empty Git repository in /tmp/.git/

2。添加测试文件

$ echo 'hello, world' > hello.txt
$ git add hello.txt
$ git commit -m "Initial commit"
[master (root-commit) 951d5cc] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

3。在.git / objects目录中显示测试文件的内容

$ git hash-object hello.txt
4b5fa63702dd96796042e92787f464e28f09f17d
$ cat .git/objects/4b/5fa63702dd96796042e92787f464e28f09f17d
xKOR04fQ(I??
$ python -c "import zlib,sys;print repr(zlib.decompress(sys.stdin.read()))" < .git/objects/4b/5fa63702dd96796042e92787f464e28f09f17d
'blob 13\x00hello, world\n'

最后一行的脚本来自this answer。

如您所见,.git / objects中的文件内容无法直接查看,cat仅将其显示为xKOR04fQ(I??。该文件也不存储在压缩数据的标准容器中,但使用zlib可以解压缩原始数据。存储的文件还包含有关内容的git元数据:git对象的类型和内容的长度。但数据显然存储在.git / objects中:

blob 13\x00hello, world\n
相关问题