How do I undo the last commit using GIT command

By | August 8, 2020

GIT has a great feature that allows us to undo the last commit. In some cases like, by mistake, you had committed the changes to GIT, or your client said that he doesn't need previous changes; in such cases, you will have to undo previous changes from GIT. It also provide us feature to change last commit's comment as well.

Undo the last commit using reset command:

You might have the above-mentioned situations or there are a lot more cases where you really need it. In these cases, GIT 'reset' command is going to help you.

$ git reset --soft HEAD~1

RESET command will rewind your current branch's specific revision as defined after the '~' symbol. The number you are seeing is specific revision number, and I have set it to 1 because I wanted to remove the last commit. You can change it to 2 if you want to remove last 2 commits.

And the '–soft' flag, will make sure that your changes are preserved and will be available as uncommitted changes in your working copy. And, if you don't want to keep changes, run below command.

$ git reset --hard HEAD~1



Category: GIT

Leave a Reply

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