什么是git提交对象的文件格式? [英] What is the file format of a git commit object?

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

问题描述

上下文:我希望能够通过我的git提交消息和提交进行搜索,而无需通过令人费解的复杂git grep命令,因此我决定查看如何存储git提交消息。



我看了一个.git文件夹,它看起来像提交存储在

  .git / objects 

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


  1. 这个乱码是什么文件格式/ git commit对象如何存储?

  2. 在这个git提交日志中,文件夹9b包含一个提交sha

      aed8a9f773efb2f498f19c31f8603b6cb2a4bc 

    为什么会有这种情况,文件中会存储多个commit sha 9B?

  3. 有没有办法将这个乱码转换成纯文本,这样我就可以在文本编辑器中搞乱提交了?



解决方案

在您进一步探索此路径之前,我可能会建议您阅读 Git手册中关于其内部的部分。我发现知道本章的内容通常是喜欢Git和讨厌它的区别。理解为什么Git按照它的方式做事往往使得它所有的那些奇怪的命令更有意义。

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


  1. 为内容创建特定于git的头文件。 $ b
  2. 生成标题+内容串联的散列。

  3. 压缩标题+内容的连接。

  4. 将压缩数据存储到名称与前两个字符相同的文件夹中数据的散列和剩余的38个字符的文件名。

这样可以回答你的第二个问题,一个文件夹将包含所有的压缩的对象以相同的两个字符开头,不管它们的内容如何。



如果您想查看blob的内容,您只需解压缩即可。如果您只想查看文件的内容,那么在大多数编程语言中,这可以轻松完成。但是,我会提醒你不要试图修改数据。修改文件中的单个字节会改变它的散列。 git中的所有元数据(即目录结构和提交)都使用散列引用进行存储,因此修改单个文件意味着您还必须更新该文件下游引用该文件散列的所有对象 。然后你必须更新引用这些散列的所有对象。继续,继续,继续......尝试实现这一目标非常迅速,非常复杂。通过学习git的内置命令,你可以节省很多时间和心痛。


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.

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

.git/objects 

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. 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
    

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

  3. 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.

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. Create a git specific header for the content.
  2. Generate a hash of the concatenation of the header + content.
  3. Compress the concatenation of the header + content.
  4. Store the compressed data to disk in a folder with a name equal to the first two characters of the data's hash and a file name with the remaining 38 characters.

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.

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提交对象的文件格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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