"git reflog" Saved The Day!

"git reflog" Saved The Day!

Ever wondered what to do when one is asked to change the commit-msg and accidentally types git --amend -m "New commit-msg"?

This merges the new changes with the previous commit and makes a whole new commit.

But then we are told to keep the review id the same (i.e keep the previous commit only)

Now how to go back to the previous state without losing our changes?

Though there can be many ways to solve it, I discovered to solve it via "git reflog".

git reflog: Reflogs track when Git refs were updated in the local repository. Using it we can reset our head to the point before we made the amend.

$ git reflog
...
d0c9f22 HEAD@{0}: commit (amend): [Feature] - ABC Commit Description 
c296452 HEAD@{1}: commit: [Feature] - ABC Commit Description

Then you can do

git reset --soft c296452

Your staging environment will now contain all the changes that you accidentally merged with the c296452 commit.

Regarding how to change the commit-msg without making an entirely new commit. We can just do :

1. $ git commit --amend
# it will open up your nano editor to change commit-msg
# after changes are done
2. CTRL+O: to save
3. Press enter
4.CTRL+X: to exit