Git Commit --amend: Correct a Private Commit
Correct the last commit message
Amend creates a replacement commit at the tip, rather than editing a commit object in place. Use it for work that nobody else relies on. In the amend lab, A records Tea and B records Rice with a vague message. Start clean; make a rescue branch pointing at B. A rescue branch preserves commits, not unstaged work.
git status --shortgit branch rescue-before-amendgit commit --amend -m "Update shopping notes"git log --oneline --all --graphgit diff rescue-before-amend HEADThe new commit has the same parent as B, a different ID because the message changed, and the same tree. The diff is empty. The old tip remains reachable via rescue-before-amend.
Add a forgotten change
Continue in the private amend fixture. Stage only the correction, review the entire staged diff, then amend with --no-edit to retain the current message. Any other staged changes would also be included.
printf "Rice\nMilk\n" > notes.txtgit add -- notes.txtgit diff --cachedgit commit --amend --no-editgit show HEAD:notes.txtThe latest snapshot now contains Rice and Milk; the new commit still has A as parent, not the replaced tip. The message remains Update shopping notes.
Inspect the recovery anchor
Do not reset or force-push just because an amend was a mistake. Compare the saved original commit with the current one and decide which content is needed. The reflog records local reference movements and can help locate prior commits, but expires and cannot promise recovery of unrecorded edits.
git show rescue-before-amend:notes.txtgit diff rescue-before-amend HEADgit reflog -n 3The saved original still contains Rice. Reflog output and abbreviated IDs vary. If a change is shared, prefer a new correcting commit after coordination.
Practice
Explain why B and its replacement are siblings rather than consecutive commits. Inspect the saved branch before any cleanup.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Commit --amend: Correct a Private Commit — knowledge check
Reason about undo operations, affected states and preservation of work.
What does amend do to the tip?
What preserves the old committed tip?
What does --amend --no-edit keep?
Why review the full staged diff before amend?
What does a reflog not guarantee?