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.
2 Answers
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)
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:
Finding what the HEAD commit was the previous is not straightforward. But this Q&A offers some possible solutions:
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.
-
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
--force
and--hard
. So if you ever use--force
or--hard
, stop for a moment and think before you type in the command.