How to remove the last commit in Git

The right command depends on one question: did you already push the commit? Start with the safe options.

Not pushed yet: undo the commit, keep your changes

git reset --soft HEAD~1

The commit is gone, and its changes are staged again. Use git reset HEAD~1 to unstage them as well.

Not pushed yet: undo the commit and throw the changes away

git reset --hard HEAD~1

Already pushed to a shared branch

Create a new commit that reverses the old one. History stays intact for everyone:

git revert HEAD
git push

Only the message is wrong

git commit --amend

Rewriting a pushed branch you own

Only if nobody else uses the branch:

git reset --hard HEAD~1
git push --force-with-lease

Warning: --hard deletes uncommitted work. If you remove a commit by mistake, git reflog lists recent states, and git reset --hard <hash> takes you back.

Questions

What does HEAD~1 mean?

It points to the commit before the current one. HEAD~3 goes back three commits.

Why is force-pushing risky?

It overwrites the remote history. Teammates who already pulled the old commit will run into conflicts.