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

In this situation: I lost some work in my git repository, this work was once completed, but now itโ€™s stuck in my story, somewhere that could not be achieved with git log - everything . The only thing I can remember is some clear line that can identify the file that is currently part of my work.

I have a solution ... but it's quite a long time, do you have a better solution?

This is my decision:

I managed to find my SHA1 command by running a few commands:

  • first find all the blob objects in .git / objects, 'git cat'ing them (and using grep) to find SHA1 from the blob containing my file.
  • Then I had to analyze all the โ€œtree-likeโ€ objects to find the file containing the SHA1 ... to the tree object that was not contained in any other โ€œtree-likeโ€ objects.
  • To finally parse all commits containing this root tree.
+2
source share
1 answer

If the commit is accessible from some ref, the best solution would certainly be to pickaxe: git log -Sstring --all .

If it is not reached, you are right, you will have to do digging. If you think you have ragged commits scattered all over the place, the easiest way is to use git fsck --lost-found to find your ragged commits. (It will also print ragged drops.) Then you can use git grep <commit> for each of these SHA1s and find your line.

On the other hand, if you think that you have several branches of candidates that have been truncated and your goal will be on one of them, I would use git reflog show to look back at your logs, find the commits that were at the ends them and recreate them so you can do git log -Sstring <branches> ^master .

+7
source

All Articles