Hone

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 --oneline
Two commits, the second one wrong.
git revert --no-edit HEAD
A third commit, whose content is the opposite of the second.
git log --oneline
Three. Nothing was removed, and the branch only moved forwards.
cat f.txt
one. The content is back to where it was.
git log -1 --format=%s
Revert "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

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.

Practise rewriting history or recording an undo on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.