There can be a lot of situations when you may want to undo the last commit in Git, that you haven’t pushed to a remote server yet.
For example, you have mistakenly committed the wrong files and want to revert the last local commit but keep the changes you made.
Or you may want to revert the last commit and cancel the changes.
Below i will show the both solutions of how to rollback the last commit but keep the changes you made and how to delete the last commit together with the last changes.
Cool Tip: Did something wrong? Want to undo everything? You can easily revert all changes back to the last commit! Read more →
Git – Undo Last Commit
Undo last commit, but keep the changes:
$ git reset HEAD~
Revert last commit and rollback the changes:
$ git reset --hard HEAD~
git reset command resets
HEAD to the specified state.
In our case it resets
HEAD~ (shorthand for
HEAD~1), that means a state that is one commit older then