Lessons · Git · undoing by adding a commit
Undo by adding
git revert HEAD makes a new commit whose diff is the exact opposite of the last one. History only grows; nothing anyone already has is rewritten.
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
It is the safe undo for anything pushed: a bad deploy, a broken change on main. Everyone pulls the revert like any other commit.
How to think about it
Find the commit in git log, revert it by hash, push. For a merge commit add -m 1 to name the mainline parent. If reverting causes a conflict, resolve it like any merge.
Worked example
git log --oneline -1The commit to undo.
git revert --no-edit HEADA new commit whose diff is the exact opposite.
git log --oneline -2Revert "..." on top; the original stays in history.
Your turn
Revert without opening the editor.
git revert HEAD
Try it in a real repository
The trap
Reverting a merge commit needs -m 1 to name the mainline parent; without it Git refuses.
Practise undoing by adding a commit on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.