9

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?

6

2 Answers 2

15

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

$ git reset HEAD@{1} 
1
  • 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 :-) Commented Mar 8, 2016 at 8:34
2

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).
1
  • you should flag duplicate questions to be closed.
    – starball
    Commented Aug 11, 2023 at 19:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.