Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: get all blobs with pattern

Tags:

git

grep

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 only the file paths and not the sha1 IDs.

like image 204
H Krishnan Avatar asked Aug 17 '11 06:08

H Krishnan


1 Answers

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.

like image 69
vman Avatar answered Nov 10 '22 00:11

vman