如何查找包含包含给定字符串的文件的树的提交SHA1 [英] How to find commit SHA1 of a tree containing a file containing a given string

查看:123
本文介绍了如何查找包含包含给定字符串的文件的树的提交SHA1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种情况:我在git仓库中失去了一些工作,这项工作曾经被提交过,但现在已经被我的历史埋没了,某处可能无法通过'git log --all'找到即可。
我唯一能记得的就是一个明确的字符串,它可以指出一个文件,这个文件是我工作的一部分。



我有一个解决方案......但它是相当长的,你有一个更好的解决方案?

这是我的解决方案:

我已经设法通过批量处理几条命令来找到我的提交SHA1:




  • 首先找到.git / (使用grep)来查找包含我的文件的Blob的SHA1。
  • 然后,我必须解析所有的'tree'对象,以找到哪些包含文件的SHA1 ...直至包含在其他树对象中的树对象。

  • 最终解析包含此根树的所有提交。


解决方案

如果提交可以从某个ref获得,那么最佳解决方案肯定是pickaxe搜索: git log -Sstring --all



如果无法访问,那么您是对的,您将不得不进行一些挖掘。如果你认为你有散落在各处的提交,最简单的做法是使用 git fsck --lost-found 来查找你的悬挂提交。 (它也会打印晃来晃去的斑点)。然后,您可以在每个这些SHA1上使用 git grep< commit> ,并找到您的字符串。



另一方面,如果你认为你有一些候选分支被修剪,并且你的目标是其中的一个,我会使用 git reflog show 查看你的reflogs,找到提交它们的提交并重新创建它们,这样你可以执行 git log -Sstring< branches> ^主人


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.

解决方案

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.

这篇关于如何查找包含包含给定字符串的文件的树的提交SHA1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆