Everyone Can Learn Program
中文 Start learning
Core · GitLesson 2 of 6Free to read

Seeing and undoing changes · Git and GitHub

How do you see what changed, and undo a mistake?

Course contents

Why you need it

The whole point of history is going back. Git has different undo commands for different situations, and picking the right one matters: some are safe on shared history, some rewrite it. Knowing which is which is what makes people trust you with their repository.

The intuition

First look: git diff shows unstaged changes line by line (+ added, - removed); git diff --staged shows what is on the tray.

Then pick the undo by where the mistake is:

Mistake is…Command
in a file, not stagedgit restore FILE
on the traygit restore --staged FILE
in the last commit, not pushedgit commit --amend
in a commit others already havegit revert COMMIT

revert doesn't erase anything: it adds a new commit that does the opposite. That's why it is safe on shared history.

Definition

Diff

The line-by-line difference between two versions. Lines starting with + were added, lines with - removed.

HEAD

The commit you are on now. `HEAD~1` is the one before it.

Examples

Look before you undo

Run it in the lesson

A change in a file: see it, then throw it away.

echo "print('debug!!!')" >> app.py
git diff
git restore app.py
git status
Step by step
  1. L2The + line is the one you just added.+print('debug!!!')
  2. L3restore puts the file back to the last commit.
  3. L4Clean again.

Revert a commit

Run it in the lesson

The last commit added a line that broke things. revert adds a new commit that removes it.

git log --oneline
git revert HEAD
git log --oneline
cat app.py
Step by step
  1. L2A new commit: Revert "add total".
  2. L3History grew; nothing was erased.
  3. L4The file is back to one line.print('shop')

Common mistakes

reset --hard on shared work

git reset --hard HEAD~1 deletes the last commit from your branch. If teammates already pulled it, their history and yours now disagree. Use revert for pushed commits.

restore throws work away

git restore FILE discards uncommitted changes in that file for good. Look at git diff first.

Quick quiz

1. You committed a typo and already pushed it. Teammates have pulled. How do you undo it?

  1. Agit reset --hard HEAD~1
  2. Bgit revert HEAD
  3. Cgit restore .
Show the answer

Answer: B

revert adds a new commit, so nobody's history is rewritten.

2. What does the last command print?

echo "print('oops')" >> app.py
git add app.py
git restore --staged app.py
git status
  1. A
    On branch main
    Changes not staged for commit:
      modified:   app.py
  2. B
    On branch main
    nothing to commit, working tree clean
  3. C
    On branch main
    Changes to be committed:
      modified:   app.py
Show the answer

Answer: A

--staged only takes it off the tray. The edit is still in the file.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictWhat's in the file?

    Type exactly what the last command prints.

  2. ModifyUndo safely

    The last commit (add total) has already been pushed. This command erases it, which would break your teammates' history. Undo it the safe way instead, so history keeps all commits.

  3. WriteClean up before committing

    Someone left two problems: config.txt was staged by mistake, and app.py has a debug line that should go. Take config.txt off the tray (keep the file), throw away the change in app.py, and do not commit anything.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Diff差异The line-by-line changes between two versions.
HEADHEADThe commit you are currently on.
Revert反转提交A new commit that undoes an earlier one.