8

I was going to add some changes using git. Instead of committing my changes, I managed to write git reset --soft ~HEAD. How do I undo this command and get my changes back?

TorK
  • 567
  • 2
  • 10
  • 27
  • Have your working files been changed? – Tim Biegeleisen Mar 08 '16 at 08:27
  • After git reset --soft ~head, my changes dissapeared and the files from another commit appeared in my git status – TorK Mar 08 '16 at 08:29
  • I managed to fix this myself. Found this command and it worked: git reset HEAD@{1}. Thank you for your time :) – TorK Mar 08 '16 at 08:33
  • 1
    https://stackoverflow.com/a/2531803/470749 and https://stackoverflow.com/a/7844566/470749 helped me – Ryan Jan 31 '19 at 23:07
  • 1
    Does this answer your question? [How to revert an unnecessary "git reset HEAD~1"](https://stackoverflow.com/questions/16944826/how-to-revert-an-unnecessary-git-reset-head1) – Dherik Aug 07 '20 at 14:06
  • Are you sure you typed "~HEAD" and not "HEAD~"? If I type "git show ~HEAD --name-only" I get "fatal: ambiguous argument '~HEAD': unknown revision or path not in the working tree." – AJM Oct 22 '21 at 13:50

2 Answers2

14

I managed to fix this myself. Found this command and it worked:

$ git reset HEAD@{1} 
dippas
  • 58,591
  • 15
  • 114
  • 126
TorK
  • 567
  • 2
  • 10
  • 27
  • 1
    I was going to suggest this but I was afraid it wouldn't work. Luckily, you didn't do `git reset --hard ~HEAD`. This would have ended badly for you :-) – Tim Biegeleisen Mar 08 '16 at 08:34
0

If you mistakenly executed git reset --soft HEAD^1 and want to undo the effect of that command, follow these steps:

  1. Find the Commit SHA: The first thing you need to do is to get the commit SHA of the commit to which you want to go back.

    Run:

    git reflog
    

    You'll see an output listing of various actions you've performed and their corresponding commit SHA. Look for the entry before the reset. It'll typically be at the top of the list, often with a HEAD@{1} descriptor next to it. Note down the commit SHA.

    The reflog output might look something like this:

    7f8e3b9 (HEAD -> main, origin/main) HEAD@{0}: reset: moving to HEAD^1
    d1a9fab HEAD@{1}: commit: your recent commit message
    ...
    

    In this example, d1a9fab is the commit SHA you're interested in.

  2. Reset Back to That Commit: Now that you have the commit SHA, you can reset back to it.

    git reset --soft d1a9fab
    

    Replace d1a9fab with whatever SHA you obtained from the reflog.

Now, you should be back to where you were before you did the accidental git reset --soft HEAD^1.

Remember:

  • --soft will keep the working directory and staging area the same. If you want to reset everything including the working directory to the way it was, use --hard (be very careful with this option as you'll lose any uncommitted changes).