0

I was using a Git tool that ran git reset -- myfile.dart and git checkout -- myfile.dart which discarded changes against my wishes.

Is there any way to restore these discarded changes?

I can't find anything in git reflog to help me restore.

1
  • If the changes weren't committed, no.
    – Schwern
    Commented Nov 25, 2022 at 3:48

1 Answer 1

0

Unfortunately there isn't a direct way, the good news though is that since your file was git added its content is still somewhere in git internal storage.


Copying the content of my answer to this other question :
Restore git files deleted after git merge --abort

Run :

git fsck --full --unreachable --no-reflog | grep blob

(note: "blob" is the internal word for "file" within git storage).

This should give you a list of internal git hashes :

unreachable blob 08bf360988858a012dab3af4e0b0ea5f370a2ae8
unreachable blob 21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89
unreachable blob 08c12ef37075732cf4645269ab5687ba6ba68943
...

note that git add myfile.dart stores the file's content, not the file's name ...

If you remember a specific string in your file, you can try to narrow your research by using git grep <string> <hash> :

$ git fsck --full --unreachable --no-reflog | grep blob | awk '{ print $3 }' > list.txt
$ cat list.txt | while read blob; do
  if git grep -q "string" $blob; then
    echo $blob
  fi
done

You can then view the whole content of a blob using :

git show $blob

and hopefully find back the file you were looking for :

git show $blob > myfile.dart

Not the answer you're looking for? Browse other questions tagged or ask your own question.