Last Updated: February 25, 2016
·
360
· klj613

Undo latest commit

Sometimes you want to undo the latest commit....

git reset HEAD~1 --hard

or...

git reset HEAD~1

if you want to keep the code changes (they will end up in the working dir).


Sometimes I use git reset HEAD~1 then git add -p to y/n hunks I agree/disagree with (like a code review). Then at the end a git diff to see the code I do not agree with.

Then to reset the branch I'd either git reset HEAD@{1} --hard or git reset origin/<branch-name> --hard (whatever points to the commit I was on).