HELP! What to do?
I made first commit. Noticed a file that is not supposed to be save with sensitive values. Did not want to get the history for this one file in there, that would be viewable still.
Proceed to revert that first commit, thinking it will go back when no files was committed.
All files are gone now as result! I cannot find in my project folder even recycle bin.
First commit? But you want to undo for a file or two? Easy peasy. No need for revert. It’s only 1 commit! Then you would not have been in this trouble you are now.
Delete the .git folder! Mind you this is before you should have done any git voodoo. Re-init git, add files, then commit. Then you’re done. You’re back in business without having so much down time.
Now going to the other solution. I have encountered this before like you. The files are still there. So to speak. Git knows it, since it has the history for that action. A simple git log will prove.
Normally, you can pick the files back one by one simply by using the checkout command. But first, get a list of these files, including full path to it. This will show all the files that have been affected or deleted in your case.
git checkout path/to/filename
For a few files, this approach works. Not very convenient when you have dozens to hundreds of files to recover. This may be your situation too. For some reason, using a wildcard will not work.
git checkout *
git checkout path/to/*
It executes but does nothing.
Sometimes commands like below will do the trick. Or it won’t. YMMV. But may be limited to *NIX-compatible OSes. For Windows, use Git Bash for one.
git ls-files -d | xargs git checkout --
In my experience, I did the following steps in git.
git checkout COMMIT_ID
git commit -m "Restore my files"
git checkout master
The first command is to get the commit ID.
Second is to checkout at that commit. Now you’ll be in limbo. You’re not back at master branch where you started out. Instead this checkout will be outside of master. You’ll see a message such as
HEAD detached at COMMIT_ID_SHORTENED
This is all good. Don’t worry. Just commit those files again. That’s the third command up there.
Then once done, checkout to master. The last command shown up there.
If for some reason there are files that conflict, the switch to master branch won’t succeed. Just add those conflicting files too.
git add path/to/file/name
git commit -m "Some more files"
git checkout master
Now all is good. Look at your folder. All files are there again.