github、SHA-1 哈希和 git 重复项

标签 github duplicates git-diff sha1

我有一个似乎有重复提交的 github 存储库。每个提交都具有相同的消息字符串和相同的日期/作者,但 SHA-1 哈希和不同。例如,在我的日志中我发现了以下四元组:

 'commit 55e55517bf32b7ba7382b97f41a1514af8a5f5dc',
 'Author: dermen <dermen@stanford.edu>',
 'Date:   Tue Feb 19 20:03:35 2013 -0800',
 'finished with the cromermann edition',
 'commit 814fb08e0d42588a500947cba42a980ac24c01b8',
 'Author: dermen <dermen@stanford.edu>',
 'Date:   Tue Feb 19 20:03:35 2013 -0800',
 'finished with the cromermann edition',
 'commit a5f581f513d12e95627669f61cfe27064ffe8319',
 'Author: dermen <dermen@stanford.edu>',
 'Date:   Tue Feb 19 20:03:35 2013 -0800',
 'finished with the cromermann edition',
 'commit a264614b674e1ad2c4c8cc953cb27cf77c0d2615',
 'Author: dermen <dermen@stanford.edu>',
 'Date:   Tue Feb 19 20:03:35 2013 -0800',
 'finished with the cromermann edition',

除 SHA-1 散列外,一切都相同。例如,当我运行时

git diff 55e55517bf32b7ba7382b97f41a1514af8a5f5dc    814fb08e0d42588a500947cba42a980ac24c01b8

我得到零输出 - 这是否意味着提交是相同的。如果这是真的,那么为什么他们会有单独的 SHA-1 哈希和?也许我理解错了,但是 SHA-1 散列不应该直接代表文件中的内容吗?因此,如果提交相等,那么它们应该具有相同的哈希值。

无论如何,我想知道尝试过滤此类明显的重复项是否明智/不明智...任何提示/重定向将不胜感激。

最佳答案

根据以下信息生成git commit sha

  • 提交信息
  • 作者签名(身份+时间戳)
  • 提交者签名(身份+时间戳)
  • tree sha(提交时目录和文件的层次结构)
  • 父提交的 shas 列表

由于 sha 不同,这是因为这些信息中至少有一个不同。

为了更好地了解每次提交的数据是什么(以及它们如何彼此不同),您可以运行以下命令来获取每次提交的原始输出

$ git show --format=raw <commit_sha>

此命令的输出示例

基于 libgit2 的随机提交项目

$ git show --format=raw eb58e2d
commit eb58e2d0be4e07c2ef873a5f0562eaa90826c2de
tree 41959050b1e3adb428e140102a0c321949be516b
parent 3b5001b4c911db9c47d62399c1adc03bd8a3ca72
parent 3e9e6cdaff8acb11399736abbf793bf2d000d037
author Vicent Marti <tanoku@gmail.com> 1371063948 +0200
committer Vicent Marti <tanoku@gmail.com> 1371063948 +0200

    Merge remote-tracking branch 'arrbee/minor-paranoia' into development

diff --cc src/refdb.c
index 359842e,4271b58..6da409a
--- a/src/refdb.c
+++ b/src/refdb.c
@@@ -86,9 -86,10 +86,10 @@@ int git_refdb_compress(git_refdb *db
        return 0;
  }

 -static void refdb_free(git_refdb *db)
 +void git_refdb__free(git_refdb *db)
  {
        refdb_free_backend(db);
+       git__memset(db, 0, sizeof(*db));
        git__free(db);
  }

回到你的问题

I get zero output - doesn't this mean the commits are identical

这意味着提交指向的内容是相同的。但是元数据肯定会有所不同。

Maybe I mis-understand, but shouldn't an SHA-1 hash directly represent content in a file?

在 Git 中,SHA-1 哈希用于表示 git 对象:blob(即文件)、(即 blob 和子树的列表)和 promise 。您可以在 9.2 Git Internals - Git Objects 一章中找到更多相关信息。 Pro Git 一书中的

For example, in my log I found the following quadruple

例如,当您修改/ rebase /修复您提交的内容时,可能会发生这种情况。在这些情况下,只有提交日期会发生变化。

In any case, I am wondering if it is wise / unwise to attempt to filter such apparent duplicates

您不必自己清理。这些对象存储在 Git 对象数据库中。 Git 实现了一种垃圾收集机制,该机制会定期自动从中删除孤立的对象(有关详细信息,请参阅 git-gc documentation )。

关于github、SHA-1 哈希和 git 重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17277721/

相关文章:

git - 一次为多个文件配置 vsdiffmerge

git - git 可以在 repo 文件和外部(非 repo )文件之间进行区分吗?

windows - 警告 : templates not found/share/git-core/templates | fatal: Unable to find remote helper for 'https'

python - 以 GitHub 为源的 CDK Codepipeline

ruby - 从嵌套数组中删除重复项

python - 如何删除 Pandas 中的配对重复?

r - 超过 n 个数字列中具有重复值的数据帧的平均行数

git - 在Git中,如何列出分支A中存在但分支B中不存在的所有文件

git - 如何将具有特定扩展名的文件从子目录添加到 git 存储库而不添加子目录本身?

java - 我收到错误“远程 URL 测试失败 : protocol 'git clone HTTPS ' is not supported"when i enter the url field