Hone

Lessons · Git · small commits are cheap to undo

Small commits are cheap to undo

Every tool on this road works one commit at a time. If a commit is an afternoon's work, so is the smallest undo you can perform.

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 also why reviewers ask for commits to be split: a commit is the unit of undo, of blame and of bisect, and packing two changes into one blunts all three.

How to think about it

Commit each finished thought. Tidy the branch before you share it if you want a neater history -- that is what rebase is for.

Worked example

echo feature > b.txt
One thing.
git add b.txt
Staged on its own.
git commit -qm 'add the b feature'
Committed on its own.
echo fix > c.txt
A different thing.
git add c.txt
Also on its own.
git commit -qm 'fix the c bug'
Two commits, two reasons.
git revert --no-edit HEAD~1
And now one of them can be undone without touching the other.
git log --oneline
Which is the whole argument.

Your turn

Undo the commit before the last one, without touching the last one.

git revert --no-edit 

The trap

Waiting until it works to commit. A save point you did not take is a save point you cannot go back to.

Practise small commits are cheap to undo on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.