Undo in Git

We are not perfect. There are may times when we make a mistake and need to revert back to the previous stage. Usually the most common way to do it would be by using Ctrl+Z.

How many times have you wanted to undo your commit? Did you ever make a spelling mistake? You did the wrong commit. If so here is the answer.

There are basically two ways to do it in Git. One is using soft reset and the other one using hard reset.

Soft Reset

This undo's your commit and keeps changes you have made in the working tree.

git reset --soft HEAD^

Hard Reset

This undo's your commit and also resets your working tree to the last commit.

git reset --hard HEAD^