Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.

时间:2023-03-09 17:06:36
Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.
git init
echo hello >> test.txt
git add test.txt

Now the blob is created but it is referenced by the index so it will no be listed with git fsck until we reset. So we reset...

git reset --hard
git fsck

you will get a dangling blob ce013625030ba8dba906f756967f9e9ca394464a

git show ce01362

will give you the file content "hello" back

To find unreferenced commits I found a tip somewhere suggesting this.

gitk --all $(git log -g --pretty=format:%h)

I have it as a tool in git gui and it is very handy.