-1

I did a commit in my local branch.
Then I realised that I committed a file by mistake.
So I did:

git reset --soft HEAD~1
git restore --staged filename.txt

Then by mistake I did git commit --amend
I realised that my changes would be merged to the previous commit so when the editor was opened I removed the messages but because I pressed save instead of quit my changes were merged to the previous commit.

Is there a way to fix the mistake or I need to do it manually? In case it helps the previous commit is a merge commit and git show does not show any code for that

Jim
  • 3,845
  • 3
  • 22
  • 47

1 Answers1

2

If you haven't done anything in the repo since then, you can just

git reset --soft HEAD@{1}

to reset HEAD back to the previous commit, i.e. the one where HEAD was before the commit --amend.

If you are unsure, you can investigate the reflog of HEAD:

git reflog show HEAD

Find the commit you want to go to. It should be the one that says

reset: moving to HEAD~1

Note that the HEAD~1 in this text does not mean the current HEAD~1, but is the literal text taken from the command that caused the mentioned reset; it is the one that you note in the question!

j6t
  • 9,150
  • 1
  • 15
  • 35