Search code examples
gitvisual-studiocommitresetundo

How to restore unstaged changes


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?


Solution

  • 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