Undo git changes using the command line

January 08, 2019

Sometimes we have extra lines of code for testing that we don't want to commit and when we are done we should undo all the changes without manually going back to the IDE and reverting the changes. Luckily, git has easy ways to discard all your changes easily.

Undo changes:

Reworking last commit; changes will stay:

git reset HEAD^

Removing the last commit; changes will be gone:

git reset --hard HEAD^

Reworking last two commits:

git reset HEAD~2

Changing your last commit:

git commit --amend