git commit对象数据结构的文件格式是什么? [英] What is the file format of a git commit object data structure?

查看:138
本文介绍了git commit对象数据结构的文件格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我希望能够搜索我的git commit消息和提交,而不必通过令人费解的git grep命令,因此我决定查看git commit消息的存储方式.

Context: I was hoping to be able to search through my git commit messages and commits without having to go through the puzzlingly complex git grep command, so I decided to see how git commit messages were stored.

我在.git文件夹中进行了查看,在我看来,提交存储在

I took a look in a .git folder, and it looks to me like commits are stored in

.git/objects 

.git objects文件夹包含一堆名称为a6和9b的文件夹.这些文件夹每个都包含一个文件名,看起来像提交2f29598814b07fea915514cfc4d05129967bf7.当我在文本编辑器中打开这些文件之一时,出现乱码.

The .git objects folder contains a bunch of folders with names like a6 and 9b. These folders each contain a file with a name that looks like a commit sha 2f29598814b07fea915514cfc4d05129967bf7. When I open one of those files in a text editor, I get gibberish.

  1. 该文件乱码是什么/如何存储git commit对象?
  2. 在此git commit日志中,文件夹9b包含一个commit sha

  1. What file format is this gibberish / How is a git commit object stored?
  2. In this git commit log, the folder 9b contains one commit sha

aed8a9f773efb2f498f19c31f8603b6cb2a4bc

为什么?在文件9b中是否存在一个以上的commit sha?

Why, and is there a case where more than one commit sha would be stored in the file 9b?

是否可以将这些乱码转换为纯文本,以便我可以在文本编辑器中弄乱提交?

is there a way to convert this gibberish into plain text so that I can mess with commits in a text editor?

推荐答案

在进一步研究此路径之前,我建议您通读

Before you head down this path much further, I might recommend that you read through the section in the Git Manual about its internals. I find that knowing the contents of this chapter is usually the difference between liking Git and hating it. Understanding why Git is doing things the way it does often makes all of the sort of weird commands it has for things make more sense.

要回答您的问题,您看到的乱码是使用zlib压缩对象后的数据.如果您在上面的链接中的对象存储"标题下查看,则可以看到有关其工作原理的一些详细信息.这是文件在git中的存储方式的简短版本:

To answer your question, the gibberish that you are seeing is the data for the object after it has been compressed using zlib. If you look under the heading "Object Storage" in the link above you can see some details about how this works. This is the short version of how files are stored in git:

  1. 为内容创建一个特定于git的标题.
  2. 生成标头+内容的串联的哈希.
  3. 压缩标题+内容的串联.
  4. 将压缩后的数据存储到磁盘上的文件夹中,该文件夹的名称等于数据散列的前两个字符,文件名其余38个字符.

为回答您的第二个问题,文件夹将包含所有以相同的两个字符开头的压缩对象,无论其内容如何.

So that answers your second question, a folder will contain all of the compressed objects that begin with the same two characters, regardless of their contents.

如果要查看blob的内容,只需将其解压缩即可.如果您只想查看文件的内容,则可以使用大多数编程语言轻松地完成此操作.但是,我警告您不要尝试修改数据.即使修改文件中的单个字节也将更改其哈希值. git中的所有元数据(即目录结构和提交)都是使用对哈希的引用存储的,因此修改单个文件意味着您还必须更新该文件下游所有引用该文件哈希的对象 .然后,您必须更新所有引用这些哈希的对象.等等,等等……试图实现这一目标变得非常非常非常复杂.您只需学习git内置的命令即可节省很多时间和心痛.

If you want to see the contents of a blob, all you have to do is decompress it. If you just want to view the contents of the file, this can be done easily enough in most programming languages. I would warn you against trying to modify data, however. Modifying even a single byte in a file will change it's hash. All of the metadata in git (namely, directory structures and commits) are stored using references to hashes, so modifying a single file means that you must also update all objects downstream from that file that reference that file's hash. Then you have to update all the objects that reference those hashes. And on, and on, and on... Trying to achieve this becomes very, very complicated very quickly. You'll save your self a lot of time and heartache by just learning git's built in commmands.

这篇关于git commit对象数据结构的文件格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆