Git: get all blobs with pattern
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"
精彩评论