DEV Community

Michael Kohl
Michael Kohl

Posted on • Originally published at citizen428.net on

Git quick tips #3: Recover deleted uncommitted files

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 file has at least been staged before:

  1. 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
    
  2. Confirm it's indeed the file we're looking for:

    $ git show e3e78fe2a762143d25831830b1ad50166560f03a
    
  3. Restore the file by redirecting the output of git show:

    $ git show e3e78fe2a762143d25831830b1ad50166560f03a > filename
    

Discussion (3)

Collapse
harry_wood profile image
harry_wood

You say the file was "not yet commited". Do you mean added but not committed, or committed but not pushed?

Collapse
citizen428 profile image
Michael Kohl Author

Added but not committed.

Collapse
muhammadmuzammilqadri profile image
Muhammad Muzammil

I think what the author meant was "added but not committed"