How do I revert to the previous commit without deleting the latest commit?


Say I have 2 commits which I have already pushed to my remote branch.

  • Commit A (31-May-2021)
  • Commit B (30-May-2021)

How do I revert to Commit B without deleting Commit A? I just want to compare the result between these 2 commits.

Note: Code comparison is not needed. I just want to compare the output of Commit A vs Commit B


I strongly disagree with the other answers advising you to use git revert. This would actually really revert the changes introduced by Commit A and produce a commit on its own.

Since you want to have a look at the state at a point back in time, you could just checkout Commit B directly so you can inspect the contents. Afterwards checkout the original branch to go back to the latest commit.

git checkout $HASH_OF_COMMIT_B   # now you are in a detached head state at commit B
git checkout $BRANCH             # now you are back at the tip of the branch (commit A)

A lot of tools let you see the difference between two references directly without the need to checkout. On the command line, this could be done with git diff:


Answered By – Matt

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published