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
