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.txtOne thing.
git add b.txtStaged on its own.
git commit -qm 'add the b feature'Committed on its own.
echo fix > c.txtA different thing.
git add c.txtAlso on its own.
git commit -qm 'fix the c bug'Two commits, two reasons.
git revert --no-edit HEAD~1And now one of them can be undone without touching the other.
git log --onelineWhich is the whole argument.
Your turn
Undo the commit before the last one, without touching the last one.
git revert --no-edit
Try it in a real repository
The trap
Waiting until it works to commit. A save point you did not take is a save point you cannot go back to.