Git和SHA-256
当前版本的 git (2.30.0) 是否已经默认使用 SHA256 来计算提交哈希?如果没有,如何为新的 git 存储库启用 SHA-256,以及如何检查某个 git 存储库是否使用 SHA-256 或 SHA-1 作为其提交哈希?
回答
在最新版本的 Git 中,是使用 SHA-1 还是 SHA-256 是每个存储库的设置。该计划最终使将数据存储在 SHA-256 存储库中并使用 SHA-1 名称或 SHA-256 名称访问对象成为可能。SHA-1 仍然是默认值。
请注意,SHA-256 模式是实验性的,理论上可以改变,但没有计划这样做。Git 开发人员正在尽一切努力不破坏与现有 SHA-256 存储库的兼容性。
要创建具有SHA-256一个新的存储库,使用--object-format选项git init。如果您想知道本地存储库使用哪种算法,请运行git rev-parse --show-object-format,它将输出sha1或sha256。要查看远程存储库的哈希值,您可以使用git ls-remote并验证打印的哈希值的长度。
请注意,没有主要伪造支持 SHA-256,因此此类存储库无法上传到它们。
回答
根据版本 2.30.0的手册页git-init,sha-256 支持仍被认为是实验性的:
--object-format=<format
Specify the given object format (hash algorithm) for the
repository. The valid values are sha1 and (if enabled) sha256.
sha1 is the default.
THIS OPTION IS EXPERIMENTAL! SHA-256 support is experimental and
still in an early stage. A SHA-256 repository will in general not
be able to share work with "regular" SHA-1 repositories. It should
be assumed that, e.g., Git internal file formats in relation to
SHA-256 repositories may change in backwards-incompatible ways.
Only use --object-format=sha256 for testing purposes.