Showing posts with label fsck. Show all posts
Showing posts with label fsck. Show all posts

09 July 2021

How to get lost files back resulting from running git add and git restore -s@ -SW?

 How to get lost files back resulting from running git add and git restore -s@ -SW?


Problem

suppose you ran

$ git add .

then you ran

$ git status

and saw that you have added some files which you did not want to add. 

(You did not run 

$ git commit

)

then you wanted to remove them from the index.

In haste, you ran 

$ git restore -s@ -SW

resulting in those files are gone away from your working tree (resulted from -W).

You want to get them back.

Solution:

Run

$ git fsck --cach --no-reflogs --lost-found --unreachable head

You will get a list of SHA1, which point to your lost files.

Then run

$ git show SHA1 > name_of_lost_file

one by one, every lost file will now be in your current work directory!