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 while has at least…


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:

  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
    


This content originally appeared on DEV Community and was authored by Michael Kohl


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Git quick tips #3: Recover deleted uncommitted files." Michael Kohl | Sciencx - Tuesday September 7, 2021, https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/
HARVARD
Michael Kohl | Sciencx Tuesday September 7, 2021 » Git quick tips #3: Recover deleted uncommitted files., viewed ,<https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/>
VANCOUVER
Michael Kohl | Sciencx - » Git quick tips #3: Recover deleted uncommitted files. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/
CHICAGO
" » Git quick tips #3: Recover deleted uncommitted files." Michael Kohl | Sciencx - Accessed . https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/
IEEE
" » Git quick tips #3: Recover deleted uncommitted files." Michael Kohl | Sciencx [Online]. Available: https://www.scien.cx/2021/09/07/git-quick-tips-3-recover-deleted-uncommitted-files/. [Accessed: ]
rf:citation
» Git quick tips #3: Recover deleted uncommitted files | Michael Kohl | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.