How to undo the most recent commits in git

Use the following command to undo the last commit:

git reset --soft HEAD~

Use the following command to undo the last two commits:

git reset --soft HEAD~2

The number behind the ~ character indicates how many commits to undo.

If you want to push the current state to a server after undoing commits, you have to force the push:

git push origin master --force