0

At first, I didn't even write git init.
And then to use AWS beanstalk(After I completed my coding),I initialize git. And I use "git add ." command. but I knew I didn't make the "gitignore" file, so I ran git reset --hard to reset the "git add", so all my files are deleted now. What should we do to restore this? "git reflog" is not working. what should I do to recover my previous code.

6
  • 1
    As a note—in general, the commands that destroy data are protected by flags like --force and --hard. So if you ever use --force or --hard, stop for a moment and think before you type in the command. Commented Jul 12, 2020 at 6:04
  • Does this answer your question? Restore git files deleted after git merge --abort
    – LeGEC
    Commented Jul 12, 2020 at 6:41
  • No, that question will likely not. Changes were commit’ed in that case (prior to the merge). After a reset —hard, and changes in the index are indeed lost as they were never committed, and you’re back to where you were. Commented Jul 12, 2020 at 6:44
  • It would have been appropriate just to add the .gitignore in the same commit (resetting any accidental additions), or to have done a reset without ‘—hard’. Use this as a learning experience and avoid annoyances in the future. (FWIW, I recommend not using “git add .”, pretty much ever..) Commented Jul 12, 2020 at 6:48
  • You will find good answers here (especially one by me ;) : stackoverflow.com/questions/11094968/…
    – Philippe
    Commented Jul 12, 2020 at 7:09

2 Answers 2

3

Since you added the files, git should still have a trace of their content.

Run :

git fsck --full --unreachable --no-reflog | grep blob

This will give you a list of hashes, each of which point to a blob (a file's content). Unfortunately, the names of the files are not stored, so you will have to make sense of what blob should be restored, and to find their names back.

Here are ways to make sense of these hashes :

You can view the content of a blob :

git show <sha>

# restoring a blob is just :
git show <sha> > filename

If you remember a specific word or instruction in a file, you can use git grep <string> <sha> on the list of blobs :

$ git fsck --full --unreachable --no-reflog | grep blob | awk '{ print $3 }' > list.txt
$ cat list.txt | while read blob; do
  if git grep -q "string" $blob; then
    echo $blob
  fi
done

You can use a trick to sort them by modification date (which should be : the moment they were added)

# using the same list.txt as above :
cat list.txt |\
    sed -e 's|^\(..\)\(.*\)|.git/objects/\1/\2|' |\
    xargs ls -l -t 2> /dev/null

(more explanations for this trick in this SO question)

4
  • That's the closest thing I know to "recovering deleted files that were only added"
    – LeGEC
    Commented Jul 12, 2020 at 7:10
  • if I follow what you told me then is it recovered? Commented Jul 12, 2020 at 12:32
  • Yes : you will get the content of your files back.
    – LeGEC
    Commented Jul 14, 2020 at 6:44
  • Run the git fsck ... command above, then pick the hash of a blob, and run git show hash > a.java. You will see what you can get back.
    – LeGEC
    Commented Jul 14, 2020 at 6:47
1

The good news is that git reset --hard is changing what Git thinks the HEAD revision for the branch is. The commit that was previously the HEAD is still in your local repo.

There are a couple of bits of bad news though:

  1. Finding what the HEAD commit was the previous is not straightforward. But this Q&A offers some possible solutions:

  2. If there were uncommitted changes when you did the git reset --hard they are not recoverable. Git only remembers stuff that you committed (or stashed).

Now, you say that git reflog is "not working". Well according to the documentation, git reset --hard doesn't affect the reflog. So I am guessing your "not working" means that it won't get back the uncommitted files. If so, I'm afraid you are out of luck. Those files are gone1.


The after-the-fact advice I would give is:

  • "commit early, commit often", and
  • make sure that you have a backup of your local git repo tree in a safe place before you embark on potentially dangerous Git procedures that you don't fully understand.

1 - Unless the missing files have been preserved in file system backups, editor / IDE buffers or history, or something similar.

3
  • I use "git add ." after using "git add ." I use "git reset --hard" due to "2. if there ~~~" does it mean I can't recover everything? Commented Jul 12, 2020 at 6:09
  • No. Nothing can be recovered in that case (the current WC is “all there is”). The index was reset, deleting the staged changes. They were never committed and thus no longer exist - there are no reflog entries if there is no commit’ish. Oops. Time to rewrite it, better. I recommend commit early, commit often, and use rebase to fix up WIP. Commented Jul 12, 2020 at 6:19
  • If the files are opened in editors, some support their own undo and backup handling.. or might allow the contents to be re-saved. Commented Jul 12, 2020 at 6:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.