This content originally appeared on DEV Community and was authored by Michael Kohl
Today I accidentally deleted a file which was not yet commited. I had a brief moment of panic because I had been working on this for a few hours but then luckily remembered that it's relatively easy to undo a mistake like this if the while has at least been staged before:
-
Use
git-fsck
to find the dangling blob:
$ git fsck --lost-found Checking object directories: 100% (256/256), done. Checking objects: 100% (252/252), done. dangling blob e3e78fe2a762143d25831830b1ad50166560f03a
-
Confirm it's indeed the file we're looking for:
$ git show e3e78fe2a762143d25831830b1ad50166560f03a
-
Restore the file by redirecting the output of
git show
:
$ git show e3e78fe2a762143d25831830b1ad50166560f03a
This content originally appeared on DEV Community and was authored by Michael Kohl

Michael Kohl | Sciencx (2021-09-07T07:49:59+00:00) Git quick tips #3: Recover deleted uncommitted files. Retrieved from https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.