Undo last commit

Ever made a change, but you forgot something

  1. Fix typo in commit message
  2. Already pushed to remote server
  3. Only undo the commit, but keep the changes
  4. Undo the commit and the changes

Fix typo in commit message

1
2
3
4
vim README.md
git add README.md
git commit -m 'This waaaaas not the intention'
git commit --amend -m 'This was not the intention'

Already pushed to remote server

1
2
3
4
5
6
vim README.md
git add README.md
git commit -m 'This was not the intention'
git push
git revert HEAD # Will do an auto commit
git push

Only undo the commit, but keep the changes

1
2
3
4
vim README.md
git add README.md
git commit -m 'This was not the intention'
git reset HEAD~

Undo the commit and the changes

There’s no way back, you’ll loose all local changes.

1
2
3
4
vim README.md
git add README.md
git commit -m 'This was not the intention'
git reset --hard HEAD~