Lessons · Git · which undo, and when
Which undo
The right undo depends on one question: has anyone else got this commit? Unpushed: reset --soft keeps the work and removes the commit. Pushed: revert adds a commit that undoes it.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Reset rewrites history and revert extends it. Rewriting history others already have breaks their copies; extending it never does.
How to think about it
Check first: git log origin/main..main lists commits only you have. Empty means pushed, so revert. Otherwise reset --soft, fix, recommit.
Worked example
git log origin/main..main --onelineEmpty means everything is pushed; anything listed is still only yours.
git reset --soft HEAD~1Unpushed: take the commit back, keep the work staged.
git revert HEADPushed: add a new commit that undoes it, history intact.
Your turn
Undo a pushed commit without rewriting history.
git HEAD
Try it in a real repository
The trap
The wrong choice is reset on pushed history: your branch and the remote disagree, the push is refused, and colleagues who pulled already have the commit.