0

Yeah I did an oopsie maybe. I hope this question gets enough attention because the title sounds like many different other questions here on Stackoverflow, but none of the answers I've found worked. But let me explain first:

(Tired,) I decided to add all my .config files to git and push them. (I was stupid+tired enough to not backup). I created a new repository with git init and added everything with git add -A. I then thought that making a .gitignore with some directories would be smart before adding and commiting everything, so I wanted to undo my git add -A.

That's where the mistake happened, I used git reset --hard instead git rm --cached * to unstage the files.

Unfortunately, git did a git rm on everything, since there was no previous commit. And that's where I am now. All config files gone. Many solutions like git update-ref -d HEAD, git reset (--hard) HEAD~1 and git reset followed by git ls-files -d -z | xargs -0 git checkout -- don't because there is no commit and no branch created yet.

I also tried to find the deleted directories and files in the .git directory or trash, but this didn't work either.

Does one of you have an idea how I could get my config files back? All I want to do is to undo the git reset...

DirkLangohr
  • 11
  • 1
  • 2
  • As matt says, nothing you can do on git level. Maaaaybe you can undelete on the OS level. You haven't tagged your OS, so I can't say anything definitive, but search for " undelete". – Amadan Jun 17 '22 at 00:15
  • Nothing on the OS level apparently. But it is okay, I have an older version still on my laptop, not everything is lost. Thank you for your answer! – DirkLangohr Jun 17 '22 at 00:58
  • 2
    There is a way to recover the "temporary" blobs created by `git add`, using `git fsck --lost-found`. It's pretty painful though: it loses all the file *names*. If you have an alternative that keeps the names, that's usually easier to use. If you still have the repository, though, go ahead and run `git fsck --lost-found`, then look in `.git/lost-found/other`. All the weirdly named files are recovered file-data. You may be able to match them up with the files of interest. – torek Jun 17 '22 at 03:24
  • There is hope because you staged the files but it will be difficult. Have a look at the first 3 answers of this question and try the solution that seems the more convenient for you : https://stackoverflow.com/questions/11094968/in-git-how-can-i-recover-a-staged-file-that-was-reverted-prior-to-committing/ – Philippe Jun 17 '22 at 05:50
  • Thank you everyone. Recovering the file names seemed harder to me then modifying the dotfiles from my laptop, so I went ahead doing this. – DirkLangohr Jun 17 '22 at 20:54

1 Answers1

0

Git is about commits. That's all it's about. If there was a "good" commit that contains the stuff you're looking for, then just find it in the reflog and check it out. But you cannot recover what was never committed.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That is not true. Recover of file content is possible even if difficult. See the comments above by Torek and me. – Philippe Jun 17 '22 at 05:54