Git:获得所有blob图案

H K*_*nan 6 git grep

在Git中,如何在对象数据库中找到包含字符串模式的所有blob的SHA-1 ID?git-grep仅提供文件路径而不提供sha1 ID.

vma*_*man 6

EDIT: Update based on new testing results using Git version 2.7.4

Looks like the solution I posted only goes through the reflog. So if you delete a reflog entry, that entry will not be searched through - even though the object still exists.

So you will have to do something like:

{
    git rev-list --objects --all --grep="text"
    git rev-list --objects -g --no-walk --all --grep="text"
    git rev-list --objects --no-walk --grep="text" \
        $(git fsck --unreachable |
          grep '^unreachable commit' |
          cut -d' ' -f3)
} | sort | uniq
Run Code Online (Sandbox Code Playgroud)

Derived from: Git - how to list ALL objects in the database

Old solution: Only works if object is in reflog

To find the string "text" in all local objects:

git log --reflog -Stext
Run Code Online (Sandbox Code Playgroud)

To find the pattern "pattern" in all local objects:

git log --reflog --grep=pattern
Run Code Online (Sandbox Code Playgroud)

This will search through all objects, so it will work even if the commit/branch is deleted. Once an object is removed from the local repository (e.g. through a gc), it will no longer be included in the search.


Von*_*onC 3

您可以使用 pickaxe 选项尝试 git log:

git log -Sstring --all
Run Code Online (Sandbox Code Playgroud)

请参阅“如何查找包含给定字符串的文件的树的提交 SHA1 ”