0

I used git reset --hard command and lost all my files. Then tried to use command git fsck which got me a dangling tree with a hash value. Then entered the git show --format=raw <hash value> which gave me list of all the lost files.

git fsck --lost-found gave me

Checking object directories: 100% (256/256), done.
notice: HEAD points to an unborn branch (master)
notice: No default references
missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling tree d2682fde785bcfd92525e5b856770c084fd32ee8

ls .git/lost-found/other gives me a hash file which when opened by the command git cat-file -p d2682fde785bcfd92525e5b856770c084fd32ee8 gives me all my lost files in this format.

040000 tree 29faf839a8e17b98198fa8e56a6abb8933a3ab78    models
040000 tree ba9e0d882162283f3590f6d3cbcdc038cf71e8a2    node_modules
100644 blob 2ff009cc3552ee4e0ac3b0babc968797f9527b90    package-lock.json
100644 blob 2b27f9894f180d2e65c468ea261c1bf96c372801    package.json
040000 tree 7254925ba8ee0d8a8313368a017be7bbc0c3a178    routes
100644 blob 6726fd46ff7359e2409a5ff9b3e5b3768fc3c31b    server.js

Please help me out on how to recover these files back to my folder.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 2
    For all the `blob`, and for example for `server.js`, it should be `git cat-file -p 6726fd46ff7359e2409a5ff9b3e5b3768fc3c31b > server.js`. But your are displaying a a tree object content that means that you should have a commit with this tree already created and that him that you should recover with the help of the `git reflog` command. The other possibility is that the content is not the one you expect and you won't recover the content that way... – Philippe May 04 '23 at 07:36
  • Does this answer your question? [How can I undo git reset --hard HEAD~1?](https://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1) – AD7six May 04 '23 at 08:20
  • No. I am still not clear on how can I recover my folders models, node_modules, package.json, routes and server.js – Ankush_anonymous May 04 '23 at 08:51
  • you get a list because that object is a `tree`. If you are able to get the root tree (which you might have many of, just in case), you might be able to restore it. – eftshift0 May 04 '23 at 08:51
  • how can i get the root tree can you help me with it – Ankush_anonymous May 04 '23 at 08:57
  • You would be able to tell by the contents.... you might script it so that it checks if a few directories are present in the tree (the one you used there looks like it.... but it might be a subdirectory, actually... I don't know how the project is laid down). – eftshift0 May 04 '23 at 09:08
  • 2
    @Philippe brought up an excelent point... if you have a tree, then a commit must have held it. You should check the reflog. – eftshift0 May 04 '23 at 09:11
  • 1
    Unrelated, but `node_modules` should usually not be tracked/committed. Commit `package.json` and let the modules directory be built by npm – knittl May 04 '23 at 09:29
  • @eftshift0 this is what i get if I do `git reflog` >> `fatal: your current branch 'master' does not have any commits yet` – Ankush_anonymous May 04 '23 at 17:57
  • @knittl please let me know how can I commit package.json which is in /.git/lost-found/others – Ankush_anonymous May 04 '23 at 18:01
  • @Ankush_anonymous by following advice of all helpful comments before. Either use `cat-file` or find the commit associated to the tree. You could also use the low-level command `commit-tree` to create a commit from the tree reference. – knittl May 04 '23 at 18:13
  • Thank you Philippe and knittl for your help I got the solution from the first comment of @Philippe itself. And I am posting the answer for the above question – Ankush_anonymous May 04 '23 at 19:15

1 Answers1

0

With discussion among the community, I ended up getting the answer to my doubt suggested by @Philippe.

After following the steps mentioned in the doubt I got the list of all lost files but could not recover them in the original format.

Coming to the answer, the answer lies in the doubt itself. Like how I accessed the hash file in .git/lost-found/other by the command git cat-file -p d2682fde785bcfd92525e5b856770c084fd32ee8 similarly we have to replace the hash value in the command with the file we want to recover with a greater than symbol and the name of the file where the content should be saved.

ie, for example, I want to recover the file server.js so the command should be

git cat-file -p 6726fd46ff7359e2409a5ff9b3e5b3768fc3c31b > server.js

This command copies the content in the hash file in a file called server.js

P.S. Take care of some points.

  1. always keep an eye on which directory you are currently in. It affects which your all commands as it decides at which location the file will be stored.

  2. the above solution is only for a blob (server.js, package.json etc.).

  3. To recover the files from a tree you need to again use the command git cat-file -p <hash-value of tree> followed by a greater than symbol > and test.txt

ie, for example I want to recover files which is in models. So the command goes like

git cat-file -p 29faf839a8e17b98198fa8e56a6abb8933a3ab78 > temp.txt

this creates a temp.txt text file which has a hash file of the file inside the models folder like this

100644 blob c76d160b60b5fd4203e181195b529acd80ca5d0b NewsItem.js

Now to access the content or code of this file repeat the steps mentioned in the answer.