How to change / revert to a previous commit in git

Lets say that your cur­rent branch has a set of com­mits in the order

where A, B, C and D rep­re­sent the check­sums of those commits.

Your head is point­ing to com­mit D, which is your lat­est com­mit. But you don’t like the changes that you made in your lat­est com­mit. You would rather go back to the state of the code in com­mit B. The sim­plest way to do that is

git reset --hard B

WARNING: This com­mand resets the stage and the work­ing direc­tory to the con­tents of the com­mit. You prob­a­bly want to git stash any changes you have in your work­ing direc­tory if you want to use them later.

NOTE: You should only do this if you have not pushed to your remote. Nor­mally any oper­a­tions that involve revert­ing to a pre­vi­ous state or rewrit­ing the his­tory 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 *