Amend
You commit, and immediately notice the typo in the message, or the file you forgot to stage. commit --amend is for exactly that moment.
It does not edit the commit — commits cannot be edited. It builds a replacement with a new id, carrying the original's parents plus whatever you have staged now, and moves your branch to it.
The commit you replaced is still in the graph but no branch points at it any more. That is what makes amend a history rewrite: safe for work only you have, disruptive once others have pulled it.
Exercises
- Exercise 1. You committed "readme" but forgot "changelog", which is still sitting unstaged. Stage it and fold it into the commit you already made, rather than adding a second one.
- Exercise 2. Amend works wherever HEAD is — even detached. Stage "extra" and amend the commit you are sitting on; notice main does not move, because it never pointed here.