复现Git commit id

How does Git create unique commit hashes, mainly the first few characters?

Git uses the following information to generate the sha-1:

  • The source tree of the commit (which unravels to all the subtrees and blobs)
  • The parent commit sha1
  • The author info (with timestamp)
  • The committer info (right, those are different!, also with timestamp)
  • The commit message

完整说明,另外可参考The anatomy of a Git commit,和 10.1 Git 内部原理 - 底层命令与上层命令对应的英文版本——10.1 Git Internals - Plumbing and Porcelain

执行以下步骤可以复现commit id——

  • git show 获得当前最新的commit信息
  • git cat-file commit HEAD
  • printf "commit %s\0" $(git cat-file commit HEAD | wc -c) 在开头增加commit len\0 文本,长度,NUL-terminated header
  • (printf "commit %s\0" $(git cat-file commit HEAD | wc -c); git cat-file commit HEAD) | sha1sum组合上面两步的内容作为SHA1算法的输入,计算获得的40个字符长度文本就是commitid信息——复现成功

第二步的结构化信息——

sha1(
    tree            => 9c435a86e664be00db0d973e981425e4a3ef3f8d (对所有tree对象的递归计算到root节点)
    parents         => [0d973e9c4353ef3f8ddb98a86e664be001425e4a]
    author(with timestamp)          => Christoph Burgdorf <christoph.burgdorf@gmail.com> Sat Nov 8 11:13:49 2014 +0100
    committer(with timestamp)        => Christoph Burgdorf <christoph.burgdorf@gmail.com> Sat Nov 8 11:13:49 2014 +0100
    commit message  => "second commit"
)

理解Git中的对象概念:Tree Object(对目录、或目录和文件的SHA-1描述。如何获取的?)

.
├── .git (contents left out)
├── assets
 |   ├── logo.png
 |   └── app.css
└── app.js
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容