Git – how to undo last commit

Sometime we need to undo last commit in git. This is fairly useful to undo a change made accidentally. Here are quick instructions to do it:

git – soft undo last commit

Undo last commit but keep the changes. This is default option. So you can also skip --soft.

$ git reset --soft HEAD~1
$ git status

In case you need to undo last two commits you can use HEAD~2 as shown below:

$ git reset --soft HEAD~2
$ git status

git – hard undo last commit

Undo last commit and discard the changes

$ git reset --hard HEAD~1
$ git status

Note that HEAD~ or HEAD~1 refers to the first parent of the tip of the current branch and HEAD~2 refers to first parent of first parent and so on.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments