-1

Originally, I wanted to squash my last 6 commits into one. So I followed this answer and ran the following:

git reset --hard HEAD~3
git reset --hard HEAD~2
git reset --hard HEAD~1

Then I saw that my code progress was gone and I was afraid that I might have lost my progress, so I wanted to undo that reset. So I followed this answer and ran:

git reset ORIG_HEAD

After this, I thought I would get to my most recent code progress, but I found 67 unstaged changes after this last reset and the code doesn't seem to have been recovered.

So, what should I do now to recover my code? I wanted to be very sure before I proceed or try other answers.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Amrovic
  • 344
  • 5
  • 13
  • Not sure, but for undo of hard reset, you need to have the commit hash of the original HEAD. – kiner_shah Mar 11 '22 at 09:38
  • You need to find the commit hash of where `HEAD` was before `git reset --hard HEAD~3` and then do `git reset --hard `. Use `git reflog` to find it. – mkrieger1 Mar 11 '22 at 09:38
  • Also, be careful, never use hard reset unless you are quite sure. – kiner_shah Mar 11 '22 at 09:39
  • Squashing is `reset --soft`. See https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard – matt Mar 11 '22 at 10:37

1 Answers1

3

By using git reflog, you can see all the commits your HEAD has been on recently. You can then use the commit-ish HEAD@{N} to come back to a previousy visited commit.

As you seem to have changed your HEAD 4 times, you should try git reset --hard HEAD@{4}.

Warning: if you try and don't succeed, it will change your reflog history as you moved your HEAD one more time, so run git reflog after each failed attempt and use the git message to identify the right commit.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65