How to change / revert to a previous commit in git

Lets say that your current branch has a set of commits in the order
A–B–C–D

where A, B, C and D represent the checksums of those commits.

Your head is pointing to commit D, which is your latest commit. But you don’t like the changes that you made in your latest commit. You would rather go back to the state of the code in commit B. The simplest way to do that is

git reset --hard B

WARNING: This command resets the stage and the working directory to the contents of the commit. You probably want to git stash any changes you have in your working directory if you want to use them later.

NOTE: You should only do this if you have not pushed to your remote. Normally any operations that involve reverting to a previous state or rewriting the history should not be done if they have been pushed to the remote.

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *