Git: get all the drops with a picture

In Git, how do I find the SHA-1 identifiers of all blocks in a database of objects that contain a string pattern? git-grep only contains file paths, not sha1 identifiers.

+4
source share
4 answers

I cannot delete this answer because it is accepted, but skaar's answer answers the OP question better. (This is still unusual, but it seems to me there are reasons why you might need it.)

-1
source

You can try the git log using the pick option:

 git log -Sstring --all 

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

+3
source

EDIT: Update Based on New Test Results Using Git Version 2.7.4

It looks like the solution I posted only goes through reflog. Therefore, if you delete the reflog entry, that entry will not be viewed - even if the object still exists.

So you will need 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 display ALL objects in the database

Old solution: only works if the object is in reflog

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

 git log --reflog -Stext 

To find the template template in all local objects:

 git log --reflog --grep=pattern 

This will search all objects, so it will work even if the commit / branch command is removed. When an object is deleted from the local repository (for example, via gc), it will no longer be included in the search.

+2
source

I did the following to find out if some of the code I wrote was lost forever or maybe was 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 will not tell you which object contains it. But, having done this, I discovered my missing code, and in the end I managed to return it.

+2
source

All Articles