3

Basically I had a codebase on my local machine, I ran:

git init

git add .

At this point, I realized I wanted to add a .gitignore file.

So I thought I needed to unstage commits, and in an amateur move, executed the following command

git reset --hard 

Now my entire code base is gone...

Is it possible to undo this!?

b.s
  • 2,409
  • 2
  • 16
  • 26
4cody
  • 344
  • 1
  • 18
  • Not without a backup or filesystem support for retrieving deleted files. You did the equivalent of `rm *`. – chepner Jul 27 '20 at 15:05
  • Ouch! It hurts to read this question. It doesn't look like there is a commit to go back to. – TTT Jul 27 '20 at 15:05
  • The content of your files is not entirely lost, but it will be a bit of a mess to reconstruct your directory of code files. Did you have many distinct files in your inital git add ? – LeGEC Jul 27 '20 at 15:06
  • 2
    Have a look at [this answer](https://stackoverflow.com/questions/39468125/restore-git-files-deleted-after-git-merge-abort/39469723#39469723) – LeGEC Jul 27 '20 at 15:08
  • Well yeah, it was a backend project in node. So from it's root folder i had a few directories with various files, models routes src/index... etc – 4cody Jul 27 '20 at 15:08
  • 2
    See also [git-recover](https://github.com/ethomson/git-recover/blob/main/git-recover). – choroba Jul 27 '20 at 15:13
  • Similar question with interesting answers: https://stackoverflow.com/questions/11094968/in-git-how-can-i-recover-a-staged-file-that-was-reverted-prior-to-committing/58853981#58853981 – Philippe Jul 27 '20 at 18:25

1 Answers1

5

git add . has added the content of your files into git ; unfortunately, it hasn't created a 'directory' structure which lists your files with their names.

What you can do :

run

git fsck --full --unreachable --no-reflog | grep blob > blobs.txt

# the content of blobs.txt will look like :
unreachable blob 08bf360988858a012dab3af4e0b0ea5f370a2ae8
unreachable blob 21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89
unreachable blob 08c12ef37075732cf4645269ab5687ba6ba68943
...

For each of these blobs, you can dump the content to disk :

cat blobs.txt | awk '{ print $3 }' | while read blobid; do
    git show $blobid > $blobid.txt
done

You will now have a list of files :

08bf360988858a012dab3af4e0b0ea5f370a2ae8.txt
21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89.txt
08c12ef37075732cf4645269ab5687ba6ba68943.txt
...

The good news is : all the files that were present in the initial git add . should be represented in there.

The bad news is :

  • this list will also contain the files you wanted to ignore initially (I assume you added your node_modules/ folder, so you would have the code for all the js libs you added ...) ,
  • you will have to figure out what files went where, and rename them to src/my_controller.js, src/model/my_model.js ...

One first filter you could apply, to try to take the node_modules/ files away is :

  • in the list of blobs above, try to find your package.json file (grep -e some_package_I_remember),
  • rename it to package.json (you may also find the lockfile), and run npm install or yarn install
  • this should create a node_modules/ folder, which looks a lot like the previous one you had
  • add that node_modules/ folder, and create a commit,
  • now, the blobs in node_modules are no longer unreachable ; re-run the git fsck ... > blobs.txt above, you should have a list of what wasn't under node_modules ...
LeGEC
  • 46,477
  • 5
  • 57
  • 104