Going back for real
By the end of this lesson you will have committed a change you knew was wrong, watched it break, and taken it back with git revert — two honest entries in your history, and nothing lost.
"Git is the undo button" was true enough to get started. It is now worth being precise, because there are three different situations that people all call "undo", they need three different commands, and one of them permanently destroys work.
Situation 1 — you changed files and have not committed
You edited, it went badly, nothing is committed. You want the last committed version back.
git restore changes.js # one file
git restore . # everything not yet committed
Your edits since the last commit are gone, for real. That is what you asked for, and it is why "commit before you experiment" is the whole habit.
Situation 2 — the bad change is already committed
You committed, and it was wrong.
git log --oneline # find the commit — the short id on the left
git revert a1b2c3d # undo it
revert does not delete the commit. It writes a new commit that is the opposite of the old one. History grows; nothing disappears.
That sounds like bureaucracy and is actually the point. The history stays true — it records that you did a thing and then undid it, which is what happened. And because nothing was rewritten, it is safe even when your code is on GitHub or someone else has it. This is the undo you will reach for most.
Situation 3 — the command that loses work
git reset --hard a1b2c3d
This moves your branch back and throws away everything after it. Commits, edits, all of it. There is no prompt, no confirmation, and no undo button for the undo button.
It has real uses. It is also the one command in this course that can lose an afternoon, so the rule is simple: if you are not certain, use revert. It gets you the same working state and it cannot cost you anything.
Look before you go back
git log --oneline -5 # the last five commits
git show HEAD # what the most recent commit changed
Two commands, ten seconds, and you are undoing something you have actually looked at rather than something you assume you remember. git show HEAD prints exactly the last commit; git diff HEAD~1 looks similar and is not the same thing — it compares your working files against the commit before last, so any edits you have not committed yet get mixed into what you are reading.
What this is for
From Build Your First Tool onward you accept changes you did not write. That is only reasonable because going back is cheap and certain. Commit before the assistant touches anything, and every change it makes becomes a proposal you can decline afterwards rather than a decision you make in advance.
Try it now
Commit a change you know is wrong: in functions.js, delete the larger function while the last line of the file still calls it, then run git add -A and git commit -m "Remove larger(): nothing calls it" (the message is wrong on purpose: something does call it). Run node functions.js and confirm it breaks. Then git revert --no-edit HEAD and confirm it works again. Now run git log --oneline and read the two entries. Notice that the history tells the truth about what you did, which is exactly what you want when the thing you broke was three weeks ago.
The prompt
After the revert, paste the history into your chat assistant:
I committed a change I knew was wrong and then undid it with git
revert. Below are git log --oneline -3 and git show HEAD. Tell me, in
one sentence each, what the two newest commits did, and whether any of
my work was lost. Do not suggest commands.
[paste both outputs]
A good answer says the older of the two removed the function, the newer put it back as a commit of its own, and nothing was lost.
Check it yourself
- After the bad commit the program stops, with a
ReferenceErrornaming the function you deleted. git revert --no-editwrites theRevert "…"message itself. Without--no-edit, git opens a text editor holding that message: save and close it to confirm. If the screen is a column of tildes, that is Vim; type:wqand press Enter.- The program runs again.
git log --onelineshows the bad commit with theRevert "…"commit above it. Nothing has disappeared.git show HEADshows the deleted function coming back, as lines marked+.
Compare with ours
Our history after the revert
$ git log --oneline -3
b41c7e2 Revert "Remove larger(): nothing calls it"
8d03f19 Remove larger(): nothing calls it
5a9e6c1 Rename the loop variables in changes.js to say what they hold
The mistake stays in the history, and so does its undoing. Three weeks from now, that is the record you want to read.
Checkpoint
Nothing more to commit: "Try it now" made both commits. Check the state rather than repeat the steps:
git log --oneline -3 # Revert "Remove larger()…" on top, the bad commit under it
git status # nothing to commit
node functions.js # runs, and prints larger's answer again
Run git revert only once. A second one reverts the revert: the newest line then reads Reapply "Remove larger()…" and the program breaks again. If that happened, one more git revert --no-edit HEAD brings it back. Then answer this lesson's questions.