Git quick tips #3: Recover deleted uncommitted files

- git productivity tips

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