git 在哪里存储 blob 对象?

Where does git store blob object?

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

如下所述post

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

更新 我只能在 .git/objects 中看到 SHA1,我认为这是对实际 blob 的引用,而不是 blob

I understand that git compress the file and then calculate the 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答案。

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

blob 13\x00hello, world\n