【问题标题】:How to find the git object id of an object with a known hash如何找到具有已知哈希的对象的 git 对象 ID
【发布时间】:2017-07-01 05:54:34
【问题描述】:

我正在使用bfg 来清理我的 git 存储库。要获取要删除的大文件列表,我使用this script。但是对于某些文件,我只想从存储库中删除它们的特定版本。

bfg 可以选择“剥离具有指定 Git 对象 ID 的 blob”。当我运行上面的脚本时,列表中的每个对象都会给我一个哈希值。鉴于该哈希,我如何找出该特定对象的 git 对象 ID,以便我可以使用 bfg 删除它?

【问题讨论】:

    标签: git bfg-repo-cleaner


    【解决方案1】:

    该脚本似乎已经列出了 git 对象 ID。

    如果您有兴趣清理特定的提交,可以使用command line "Which commit has this blob?" 检查特定对象 ID 是否是所述提交的一部分。

    git log --all --pretty=format:%H -- <path> | \
     xargs -n1 -I% sh -c "git ls-tree % <path> | \
     grep -q <hash> && echo %"
    

    例如,在我的repo seec

    a255b5c1d469591037e4eacd0d7f4599febf2574 12884 seec.go
    a7320d8c0c3c38d1a40c63a873765e31504947ff 12928 seec.go
    

    我要清理a7320d8版本的seec.go

    如在 BFG commit 12d1b00 中看到的:

    人们可以使用以下方式获取 blob-id 列表 “git rev-list --all --objects”,然后 grep 列出他们想要核对的目录中的所有文件,并将其传递给 BFG。

    注意:bi test 读作:

    val blobIdsFile = Path.createTempFile()
    blobIdsFile.writeStrings(badBlobs.map(_.name()),"\n")
    run(s"--strip-blobs-with-ids ${blobIdsFile.path}")
    

    表示-bi 的参数是一个文件,其中包含blob id。


    我还可以通过查找提交来检查我刚刚得到的确实是 blob id:

    vonc@bvonc MINGW64 ~/data/git/seec (master)
    $ git log --all --pretty=format:%H -- seec.go | xargs -n1 -I% sh -c "git ls-tree % seec.go|\
    grep -q a7320d8 && echo %"
    

    我得到:commit c084402

    让我们看看该提交是否确实包含 seec.go 修订 blob id a7320d8(使用“Git - finding the SHA1 of an individual file in the index”)。
    我可以从 GitHub 提交中找到文件的 blob id:

    vonc@bvonc MINGW64 ~/data/git/seec (master)
    $ (echo -ne "blob $(curl -s https://raw.githubusercontent.com/VonC/seec/c084402/seec.go --stderr -|wc -c)\0"; \
       curl -s https://raw.githubusercontent.com/VonC/seec/c084402/seec.go --stderr -) | \
      sha1sum | awk '{ print $1 }'
    a7320d8c0c3c38d1a40c63a873765e31504947ff
    

    宾果游戏。

    如果我想去掉 seec.go blob id a7320d8,我知道我可以将该 blob id 传递给 bfg(在“blob ids”文件中)。

    【讨论】:

    • 我链接的脚本的哈希似乎是文件内容的 SHA-1,而不是哈希 id,所以当我将它与 bfg 的 --strip-blobs-with-ids 选项一起使用时,它找不到文件。
    • 您链接的脚本是关于 blob id 的,因为我设法从具有脚本返回的 id 的提交中获取 blob id。
    • 这很奇怪。没错,我在脚本中得到的哈希确实是对象 ID 本身(我做了git rev-list --all --objects | grep myFile 并在列表中看到了哈希)。然而,当我使用 java -jar bfg-1.12.15.jar -bi 12345 将该哈希传递给 bfg 时,它会显示 Error: Option --strip-blobs-with-ids failed when given '12345'. 12345(The system cannot find the file specified).(12345 是示例哈希)
    • @Chin 你可以看到测试中使用的选项:github.com/rtyley/bfg-repo-cleaner/blob/…--strip-blobs-with-ids ${blobIdsFile.path} 它必须需要一个包含 blob id 的文件。
    猜你喜欢
    • 2021-07-01
    • 2011-08-26
    • 2018-07-24
    • 2022-08-19
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多