Sometimes we have an existing folder, ‘git init’ and ‘git add .’ ready to commit.
Then we realize some unnecessary files have been added.
In this situation, usually using ‘git reset –soft’ to reset the stage status.
BUT we might use ‘git reset –hard’ by accident…
Some articles said we can use ‘git reflog’ to find out the lost commits.
However, because our git repo is the initialed one without any commit so reflog might be useless.
If using ‘git reset –hard’ at very beginning then want to rescue files back, this article might be help.
Scenario
Assume that we have a folder Experiment and there is 3 files inside. Which is Demo1.txt Demo2.txt ApiController.cs
Then:
Using
git init
Using
git add .
Using
for demo and we can see those files are gone.git reset --hard
Using
then realize does not have any commitgit reflog
So how do we rescue the files?
First : Collect all dangling files to lost-found folders
We can collect all dangling files by this command git fsck --lost-found
After that, it’ll show those dangling files. Go to .git/lost-found/other
Second : Grep the keyword from those dangling files
Using grep command to search the keyword. EX : grep -rw “Demo1”
So we can make sure 85002821eb3812840…. is the Demo1.txt
We can gerp ApiController, too.
Third : Copy to the original folder and rename it
Finally, just copy those files to the original path and rename it, and Bob’s your uncle.