🚀 Alguni Start learning

Unit 4: Undo

Every kind of taking it back.

Unit 4 of 8 in Git and version control for kids. Its 5 lessons are Throw a Change Away, Take It Out of the Box, Fetch an Old Version, Undo a Whole Commit and Rescue Mission — below is everything each one explains, and a question or two from it to try.

Every terminal session on this page was replayed before it shipped, on a git whose every message is tested against the real one.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🧹 Throw a Change Away

The safety net

You changed a file, you hate it, and you have not committed. git restore <file> copies the last saved version back over it.

This really does throw your edit away. That is fine — the whole point of committing often is that you never lose more than a few minutes.

Terminal

$ cat story.txt
aaaaargh
$ git restore story.txt
$ cat story.txt
A good sentence.

Try it yourself

What does git restore notes.txt do?

  • Deletes notes.txt
  • Puts the last committed version of notes.txt back
  • Saves notes.txt to a commit
  • Renames the file

Careful: what happens to the edit you had made?

  • It is kept somewhere safe
  • It is gone — git never saw it, so it cannot bring it back
  • It goes into the staging area
  • It becomes a new commit

Answer them in the app

📤 Take It Out of the Box

Staged by mistake

You ran git add on something you did not mean to. git restore --staged <file> takes it back out of the staging area.

Your edit is not touched. It just stops being part of the next commit. Git even suggests this command in git status.

Terminal

$ git status --short
M  notes.txt
$ git restore --staged notes.txt
$ git status --short
 M notes.txt

Try it yourself

Which pair is right?

  • restore = unstage, restore --staged = throw away
  • restore = throw away the edit, restore --staged = unstage it
  • They do the same thing
  • Neither of them is a real command

Answer it in the app

⏳ Fetch an Old Version

Time travel for one file

Sometimes you only want *one* file the way it was three commits ago, without disturbing anything else.

git restore --source=HEAD~2 story.txt pulls that one file out of that one commit and drops it on your desk. Nothing is committed until you say so.

Terminal

$ cat story.txt
version two
$ git restore --source=HEAD~1 story.txt
$ cat story.txt
version one
$ git status --short
 M story.txt

Try it yourself

After git restore --source=HEAD~1 story.txt, what is true?

  • The old version is on your desk, not yet committed
  • The last commit was deleted
  • You are now on a different branch
  • Nothing changed at all

Answer it in the app

↩️ Undo a Whole Commit

Undo by doing the opposite

A commit you already made cannot be rubbed out safely — other people may have it.

So git revert makes a new commit that does the opposite. The mistake stays in the history, with its undo right after it. Honest, and impossible to get wrong.

--no-edit just means "use the message git suggests".

Terminal

$ git revert --no-edit HEAD
[main b6942f9] Revert "Add adverts"
 1 file changed, 1 deletion(-)
$ git log --oneline
b6942f9 (HEAD -> main) Revert "Add adverts"
00627a4 Add adverts
d401b5b Add the page
$ cat page.txt
hello

The dangerous cousin

git reset --hard HEAD~1 really does delete the last commit, and any uncommitted work with it.

It is useful on your own machine, for a commit you made two seconds ago and nobody has seen. Anywhere else, use revert — reset can lose work that nothing can bring back.

Try it yourself

After a revert, how many commits are in the history?

  • One fewer — the bad one was removed
  • One more — the bad one plus its undo
  • The same
  • None, it starts over

You pushed a bad commit and your friend already has it. What do you use?

  • git reset --hard, it is faster
  • git revert, so their history still matches yours
  • Delete the repo
  • Nothing can be done

Answer them in the app

🏆 Rescue Mission

Try it yourself

Edit not staged, and you hate it. Which command?

  • git restore file.txt
  • git restore --staged file.txt
  • git revert HEAD
  • git commit -m "oops"

Staged by mistake, but you want to keep the edit. Which command?

  • git restore file.txt
  • git restore --staged file.txt
  • git reset --hard
  • git init

Answer them in the app