A recent deployment caused a serious issue. How do you undo the last commit?

git reset --hard HEAD~1 moves the current branch pointer back one commit and discards that commit's changes from both the working directory and the index, effectively removing it from local history entirely.

Key Points

Example

A developer commits a change locally, realizes immediately it's broken, and hasn't pushed yet, so they run git reset --hard HEAD~1 to cleanly discard that commit before anyone else ever sees it.

git reset --hard HEAD~1

Interview Tip

A concise interview answer is

If the commit hasn't been shared yet, git reset --hard HEAD~1 cleanly removes it and its changes from history. But if it's already pushed, I'd use git revert instead, since it undoes the change with a new commit without rewriting history that others may already have.

Last updated: 11 November 2026

Previous question: You need to fix an urgent bug but aren't ready to commit your current work. How do you store your changes temporarily and get them back later?

More Git - Level I interview questions

All Git - Level I questions