问题描述
我的存储库中有一个文件,我不小心将其重置为git reset --hard
.该文件仍在存储库中,但是由于未提交更改,因此无法访问该文件.
I have a file in my repository which I accidentally resetted as git reset --hard
. The file is still in the repository, but as the change wasn't committed I cannot get to it.
我发现了一些相关问题:
I found some related questions:
- Recovering added file after doing git reset --hard HEAD^
- Accidentally reverted to master, lost uncommitted changes
但是,我只发现了一些旧的丢失的东西,但没有发现最近的东西.有没有一种方法可以仅对此特定文件进行grep
历史记录?
However with these I just found only some old lost stuff, but not this recent one. Is there a way to kind of grep
the history just for this specific file?
可能有帮助的是,我知道丢失文件的内容.也许我可以grep
悬而未决的斑点/提交与此内容?
What could help is that I know content of the lost file. Maybe if I could grep
the dangling blobs/commits with this content?
推荐答案
您可以
git fsck --full --cache --dangling
并查找dangling blob
条目-其中之一应该是丢失的文件.
and look for dangling blob
entries - one of them should be your lost file.
您可以尝试grep(为确保确定,在这里我们选择更大的一组未引用对象):
You can try to grep (here we select a bigger set of unreferenced object, just to be sure):
git fsck --unreachable --no-reflogs --full 2>/dev/null | awk '$2 == "blob" {print $3}' | xargs git grep -a your-search-string
它将打印找到的斑点的SHA-1以及匹配的行.
It will print SHA-1 of the found blob along with the matched line.
有了哈希后,请使用git show <hash>
来查看文件.
Once you have the hash, use git show <hash>
to see the file.
这篇关于Git:撤消未提交的更改以重置特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!