0

I am using Visual Studio for git-stuff. And I somehow lost my changes, which I want to restore.

These are the steps I performed:

1) Using VS: Made a commit, with wrong files: "WrongCommit"

I noticed, that the commit was wrong and I simply wanted to undo it, so that my project was again ahead of the repository and provided changes to commit.

2) Using VS: Clicked Revert: "RevertCommit"

Now there were two commits listed in VS: "WrongCommit" and "RevertCommit". And the local project suddenly was resetted to the state before "WrongCommit". So I lost all my changes, but I figured they must still be contained in the "WrongCommit".

I then found this answer "Stackoverflow: Undo a commit and redo":

$ git reset HEAD~                                          # (2)

The answer states (as far as I understood), that the command "[..] undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit [..]". However I do not have any changes to commit now.

Here is the exact statement history I used (Image: Git Commands), where you can see, that git even says 'Unstaged changes after reset', but these changes are just gone.

How can I still recover those changes?

RUL
  • 268
  • 2
  • 12

1 Answers1

0

Run git reflog ScreenViewer to find the commit hash of WrongCommit. Suppose it's abc123. Then run:

git reset abc123 --hard
git reset HEAD~
git status
#modify files if necessary, and then git add the right files and git commit
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Thanks! This put me on the right track, alltough I did `reset HEAD@{7} --soft`. And what's the difference between `HEAD~1` and `HEAD@{1}` anyway? – RUL Aug 15 '18 at 09:16
  • @RUL see https://www.git-scm.com/docs/gitrevisions.html#gitrevisions-emltrevgtltngtemegemmaster3em and https://www.git-scm.com/docs/gitrevisions.html#gitrevisions-emltrefnamegtltngtemegemmaster1em – ElpieKay Aug 15 '18 at 09:20
  • Allright. `HEAD~1` means the last commit, wherease `HEAD@` is there to refere directly to the reflog lines. – RUL Aug 15 '18 at 09:28