Lesson 2 of 6 in Undoing Things
Fixing the last commit
Story: A typo in the newest record. Fix it before anyone reads it.
You've just committed, and then you spot a typo in the message. Or a file you forgot to add. You don't need a second commit to fix it: git commit --amend replaces the last commit.
Fix the message
git commit --amend -m "Add the login page"
Without -m, git opens the old message in the edit pane, so you can change it, or save it as it is.
Add what you forgot
--amend builds its commit from the index, like any commit. So stage the missing file first, then amend:
git add login.css
git commit --amend -m "Add the login page"
Taking a file out works the same way. Stage its removal with git rm --cached <file>, which keeps the file on your disk, then amend.
Amend makes a new commit
A commit can never change. Amend makes a new commit with the same parent as the old one, and moves your branch to it. Watch the Live Graph: the new commit has a new id, and the old one turns faded, because no branch points at it any more.
The old commit isn't deleted straight away. git remembers where each branch has been, and main@{1} means "where main was one move ago". Rescuing lost work shows how to use that.
Only amend what you haven't shared
Amend only ever replaces the last commit on your branch. And because it swaps that commit for a new one, only amend commits you haven't pushed yet. Anyone who already has the old commit would end up with a history that no longer matches yours. Undoing in public shows the safe way to undo a commit others already have.
New commands
git commit --amend
Explainer: Amend replaces the last commit
Graph as text
project: your repo, you are here
- HEAD → main at 18b1aef "Add the login page"
Branches and tags
- main at 18b1aef "Add the login page"
Commits, newest first
- 18b1aef "Add the login page"; parent 3bfd209; refs: HEAD → main
- 3bfd209 "Add the home page"; no parent
Unreachable
- 294cf91 "Add the login page"; parent 3bfd209
- 414e037 "Add the lgoin page"; parent 3bfd209
Challenge 1 of 3
Story: A misspelt record is a misfiled record.
The last commit's message has a typo: Add the lgoin page. Change the message to Add the login page, without adding a new commit.
Unlocks after Branching & Merging.
Terminal
Unlocks after Branching & Merging.
This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.