1

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:

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?

EDIT: What could help is that I know content of the lost file. Maybe if I could grep the dangling blobs/commits with this content?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vojtěch
  • 11,312
  • 31
  • 103
  • 173

2 Answers2

0

Git does store objects in its database. You MAY be able to find the file by doing this:

find .git/objects/ -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort

You should get a list of objects by date. Each entry will end with a sha. Take each sha (i.e. d670460b4b4aece5915caf5c68d12f560a9fe3e4), and enter:

git show <the sha>

You may have to try several before you find the correct file.

My answer is derived from https://stackoverflow.com/a/7376959/4374801

Community
  • 1
  • 1
Roger Creasy
  • 1,419
  • 2
  • 19
  • 35
0

You can do

git fsck --full --cache --dangling

and look for dangling blob entries - one of them should be your lost file.

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

It will print SHA-1 of the found blob along with the matched line.

Once you have the hash, use git show <hash> to see the file.

Roman
  • 6,486
  • 2
  • 23
  • 41