git - 如何找到提交的相应树哈希和 blob 哈希?

标签 git

我正在学习 git,并且知道每个提交都指向一棵树,而树又指向相应的 blob。 在执行 git 提交时,我可以看到生成的提交 ID。但是如何找到与提交相关的相应树哈希和 blob(s) 哈希?

感谢您的回复。

最佳答案

您可以使用 git cat-file -p 关于各种类型的对象。

git-cat-file - Provide content or type and size information for repository objects
...
-p
Pretty-print the contents of <object> based on its type.

这是一个例子。

在我的仓库中,最上面的提交是这样的:

❯ git lg -1
* 87ffcaa: (7 weeks ago) Updated all packages (HEAD -> develop, origin/develop)
| Lasse Vågsæther Karlsen <lasse@vkarlsen.no> (Tue, 16 Jun 2020 12:30:21 +0200)

为了查看该提交的内容,我可以执行以下操作:

❯ git cat-file -p 87ffcaa
tree dedfc8120583a89936cacf2e55c5db1d6d532129
parent def5a89a442d0ec88243a43ca9c9fef493dbf4c6
author Lasse Vågsæther Karlsen <lasse@vkarlsen.no> 1592303421 +0200
committer Lasse Vågsæther Karlsen <lasse@vkarlsen.no> 1592303421 +0200

Updated all packages

现在,如果我想查看它引用的树:

❯ git cat-file -p dedfc8120583a89936cacf2e55c5db1d6d532129
100644 blob 9a277a1791951e14c235243bcf374ee2d01e27b9    .editorconfig
100644 blob 1ff0c423042b46cb1d617b81efb715defbe8054d    .gitattributes
100644 blob 304b515a2ae6795ef7c73c6f0d1e822fcefd66b3    .gitignore
040000 tree 74fd33ec96af2b083e9367448bd6f6786e8b4d47    ConsoleSandbox
100644 blob e47b11709137791b3c1241092b3d5215df834ad3    LICENSE
100644 blob 2f2aaad1b5662e3d3e33398c6ce29106d0fbdcac    LVK.sln
100644 blob 9ec3d74ae05f906457f6a60ffa2df78a070ffabb    LVK.sln.DotSettings
100644 blob a1f9a1de838feeec888ab34bf35a998ade9c3560    README.md
040000 tree 759ef337f68ec31f0ca035f9427f309e87765030    SolutionQualityAssuranceTests
...

如果我想查看其中的实际 blob 之一,例如 LICENSE 文件:

❯ git cat-file -p e47b11709137791b3c1241092b3d5215df834ad3
MIT License

Copyright (c) 2018 Lasse Vågsæther Karlsen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
...

您也可以使用它来挖掘子文件夹。上面的树在下面有这一行:

040000 tree 408c62b058b6f4b14f65188d492ec452f0bb6e1d    src

这是一个子文件夹。我可以用同样的方式展示这个引用的树:

❯ git cat-file -p 408c62b058b6f4b14f65188d492ec452f0bb6e1d
100644 blob af6a33f6fe9f8f35d7d070fa16be01319df801bd    Directory.Build.props
040000 tree 9a8adfce1ccb494689fd6710369c1f925fa8acbf    LVK.AppCore.Console
040000 tree 4054753cad506b9a0132c72becd906b388bc8e59    LVK.AppCore.Windows.Forms
...

等等。

关于git - 如何找到提交的相应树哈希和 blob 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63208596/

相关文章:

git describe --tags --long 向哈希添加字符?

git rebase 致命 : Needed a single revision

git - VSTS 发布 pull 请求构建触发器

git - `git stash show` 的输出为空

git - 如何让更早的提交显示在 openshift 上?

iphone - Git中如何解决.xib文件冲突

Git 推送错误 : Unable to unlink old (Permission denied)

git - 为什么 vsdiffmerge 总是打开一个新的 VisualStudio 而不显示差异

Gitblit push rejected 原因未在 intellij-idea 中显示

svn - 使用 Git 或 Hg,如果整个团队都使用来自中央服务器的 pull 和推送,它与 SVN 有何不同?