开发者

Git: get all blobs with pattern

开发者 https://www.devze.com 2023-03-28 10:19 出处:网络
In Git, how do I find the SHA-1 IDs of all blobs in the object database that contain a string pattern? git-grep provides开发者_C百科 only the file paths and not the sha1 IDs.EDIT: Update based on new

In Git, how do I find the SHA-1 IDs of all blobs in the object database that contain a string pattern? git-grep provides开发者_C百科 only the file paths and not the sha1 IDs.


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

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

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

git log --reflog --grep=pattern

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.


I did the following to figure out if some code I'd written was lost forever, or was perhaps hidden in some "unreachable" commit:

# Assuming you're at the root of the git repository
> cd .git/objects
# Search all objects for a given string (assuming you're in .git/objects)
> find ?? -type f | sed s!/!! | git cat-file --batch | grep --binary-files=text <SEARCH_STRING>

This will produce output if any git object contains <SEARCH_STRING>. However, it won't tell you which object contains it. But by doing this, I found my missing code and I was eventually able to get it back.


You can try a git log using the pickaxe option:

git log -Sstring --all

See "How to find commit SHA1 of a tree containing a file containing a given string"

0

精彩评论

暂无评论...
验证码 换一张
取 消