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

隐身守侯 提交于 2019-12-12 22:31:33

问题


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 that 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.

回答1:


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.



来源:https://stackoverflow.com/questions/3451232/how-to-find-commit-sha1-of-a-tree-containing-a-file-containing-a-given-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!