0

I accidentally used this command on my main branch

git reset --hard origin/main

and now all my work of previous 3 days gone. Unfortunately ,I couldn't commit in last few days and now all the work of previous days is gone and I dont have another backup for that. Is there any way I can get it back. Yesterday I committed but could'nt push due to some error now When I use

git reflog

it is showing

b1a3b94 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: IgnoreUpdated
9d9ad52 HEAD@{1}: reset: moving to origin/main
9397cfe HEAD@{2}: commit: 28122020
08e9bc4 HEAD@{3}: commit: 27122020
d7436a7 HEAD@{4}: commit: 27122020
0427a52 HEAD@{5}: commit: 27122020
e98e686 HEAD@{6}: commit: 23122020

now can i get any files back from HEAD@{3}

Apratim
  • 204
  • 2
  • 9
Benson OO
  • 477
  • 5
  • 22
  • 2
    Does this answer your question? [Recover from git reset --hard?](https://stackoverflow.com/questions/5788037/recover-from-git-reset-hard) – 1615903 Dec 28 '20 at 09:40
  • The commits can be recovered (if you act now). The changes you didn't commit are lost (unless you have a backup). – axiac Dec 28 '20 at 09:40
  • 1
    Before you do anything further, back up your folder. – Mateen Ulhaq Dec 28 '20 at 09:42
  • 3
    _"I Couldn't Commit in last few days"_ -- nothing can prevent you to commit your work in Git. Unlike other VCS-es that require network connection to commit (because the committed data is sent to the central repository), Git allows and even recommends committing as often as needed. For most of its operations it does not require a network connection; there is no reason to not commit your work. – axiac Dec 28 '20 at 09:44
  • i comitted but could'nt push yesterday can i get it back – Benson OO Dec 28 '20 at 09:52
  • kindly see my edited question – Benson OO Dec 28 '20 at 09:56
  • Git stores commits. It does not store files, though commits themselves *do* store files, so once you have a commit, that commit saves the files that are in it. So as long as you did actually make a commit, containing the files you'd like to get back, you can get the files back. If you did not make a commit with the files you'd like to get back, Git cannot help you get those files back, because Git stores *commits*. The files that you see and work with are not actually *in* Git: they are *files* and Git does not store *files*. – torek Dec 28 '20 at 20:21

1 Answers1

2

Unsurprisingly, you simply git reset --hard back to the commit you want. Before doing so I recommend creating a new local branch in case you'll pick the wrong commit or will change your mind.

It would be prudent to store your current state of git reflog - your next actions will add more lines in there and if you really use commit messages like "27122020", then you will have have trouble finding your way around.

git reflog > ~/my-emergency-reflog-backup.txt
git checkout -b recover-work
git reset --hard 08e9bc4
Patryk Obara
  • 1,847
  • 14
  • 19