Lessons · Git · rewriting history or recording an undo
Rewriting history, or recording an undo
reset moves the branch backwards and pretends the commit never happened. revert adds a NEW commit that undoes an old one, and leaves the history intact.
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
One of them is safe to push and the other is not, and which you need depends entirely on whether anybody else has the commit yet.
How to think about it
Alone with it: reset. Shared: revert. The question is never which command is better, it is who else has already seen this.
Worked example
git log --onelineTwo commits, the second one wrong.
git revert --no-edit HEADA third commit, whose content is the opposite of the second.
git log --onelineThree. Nothing was removed, and the branch only moved forwards.
cat f.txtone. The content is back to where it was.
git log -1 --format=%sRevert "the bad change" -- the history says what happened and why.
Your turn
Undo the last commit with a new commit, without editing a message.
git --no-edit HEAD
Try it in a real repository
The trap
Reverting on a branch nobody else has. It works, and it leaves two commits where none were needed. Reset is tidier when it is safe.