3

I have an existing java project and i did the following

cd existing_folder
git init
git remote add origin URL
git add .
git reset --hard

I made accidentally did git reset --hard is there any way to recover my data back

that's it deleted all of my directories and files of my project

phd
  • 82,685
  • 13
  • 120
  • 165
mani deep
  • 33
  • 1
  • 4
  • I'm afraid you will not be able to get your files back through git. – Henridv Jan 21 '20 at 10:44
  • Does this answer your question? [Recover files in git after reset --hard on added files(not committed!)](https://stackoverflow.com/questions/22303293/recover-files-in-git-after-reset-hard-on-added-filesnot-committed) – phd Jan 21 '20 at 12:48
  • https://stackoverflow.com/search?q=%5Bgit%5D+recover+files+hard+reset – phd Jan 21 '20 at 12:48

4 Answers4

1

There is only way to find content of files that was added to index:

find .git/objects -type f | sed 's/.git\/objects\///g' | sed 's/\///g' | xargs -n 1 git cat-file -p

Hope this helps! You can find additional documentation on git object in official docs

stck
  • 72
  • 1
  • 11
  • 1
    Not sure who downvoted this answer, as it should work (at least on Unix-like systems). However, it will put all the content into one big stream on stdout. – torek Jan 21 '20 at 17:37
1

You can try to recover from index objects. I think you are looking for this.

Previously staged changes (git add) should be recoverable from index objects

git reset HEAD@{0}

You can use the git reflog show to list the index

git reflog show

Output will be like a

93567ad HEAD@{0}: reset: moving to HEAD@{6}    
203e84e HEAD@{1}: reset: moving to HEAD@{1}    
9937a76 HEAD@{2}: reset: moving to HEAD@{2}
203e84e HEAD@{3}: checkout: moving from master to master
203e84e HEAD@{4}: reset: moving to HEAD~1
9937a76 HEAD@{5}: reset: moving to HEAD~1
d5bb59f HEAD@{6}: reset: moving to HEAD~1
9300f9d HEAD@{7}: commit: fix-bug

More detailed info Recover from git reset --hard?

Also you can write the object in .git/lost-found/ with git fsck --lost-found .There you can use git show <filename> to see the contents of each file.

Lalit Mohan
  • 2,685
  • 2
  • 16
  • 16
0

git reset --hard resets the working to the last commit. Unfortunately, there's no way in Git to undo this operation, so unless you have some other form of back-up, you're out of luck.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

See Recover files in git after reset --hard on added files(not committed!). There's a bit of a short-cut you can use: instead of just:

git fsck

which prints a bunch of dangling blob messages that you must save and check as shown in its accepted answer, use:

git fsck --lost-found

This time, instead of just saying dangling blob *hash, Git will extract the contents of that "dangling blob" file into .git/lost-found/other. The name of the extracted file will be the blob hash ID. The content will be either one of the files you git add-ed with git add ., or some other content left over from some other earlier operation that added content but ended up not using it. (In this case, since the Git repository itself is new, there should not be any such files.)

Unfortunately, the names of the files are all lost: they were in the index, but the index has now been re-set via git reset and no longer has the files' correct names. If you can map from content back to file name manually, you can move or copy the files from the .git/lost-found/other directory to exist under whatever names you had originally.

(When you are all done, you can remove the contents, if any, of .git/lost-found/commits and anything remaining in .git/lost-found/other. In your case there should be nothing in .git/lost-found/commits.)

torek
  • 448,244
  • 59
  • 642
  • 775