How to find commit SHA1 of a tree containing a file containing a given string
This is the situation: I've lost some work in my git repository, this work was once commited, but is now burried in my history, somewhere that might be unreachable by 'git log --all'. The only thing I've can remember is some distinct string that could pinpoint a file that is part of my work at this time.
I've got a solution... but it is quite long, have you a better solution ?
This is my solution:
I've managed to find my commit SHA1 by batching several command:
- first finding all 'blob' objects in the .git/objects, 'git cat'ing them (and using grep) to find the SHA1 of the blob tha开发者_如何学编程t contained my file.
- Then I had to parse all 'tree' objects to find which contained the SHA1 of the file... up to the tree object that was contained in no other 'tree' objects.
- To finally parse all commit that contained this root tree.
If the commit is reachable from some ref, the optimal solution would pretty definitely be the pickaxe search: git log -Sstring --all
.
If it's not reachable, you're right, you're going to have to do some digging. If you think you have dangling commits scattered all over the place, the easiest thing to do would be to use git fsck --lost-found
to find your dangling commits. (It'll also print dangling blobs.) You can then use git grep <commit>
on each of these SHA1s, and find your string.
On the other hand, if you think that you have a few candidate branches that got pruned, and your target will be on one of them, I would use git reflog show
to look back into your reflogs, find the commits that were at the tips of them, and recreate them, so you can do git log -Sstring <branches> ^master
.
精彩评论