Seeing and undoing changes · Git and GitHub
How do you see what changed, and undo a mistake?
Course contentsWhy 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 staged | git restore FILE |
| on the tray | git restore --staged FILE |
| in the last commit, not pushed | git commit --amend |
| in a commit others already have | git 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 lessonA change in a file: see it, then throw it away.
echo "print('debug!!!')" >> app.py
git diff
git restore app.py
git status- L2The + line is the one you just added.
+print('debug!!!') - L3restore puts the file back to the last commit.
- L4Clean again.
Revert a commit
Run it in the lessonThe 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- L2A new commit: Revert "add total".
- L3History grew; nothing was erased.
- 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?
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 statusShow 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.
- PredictWhat's in the file?
Type exactly what the last command prints.
- 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. - WriteClean up before committing
Someone left two problems:
config.txtwas staged by mistake, andapp.pyhas a debug line that should go. Takeconfig.txtoff the tray (keep the file), throw away the change inapp.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
| Term | Chinese | Definition |
|---|---|---|
| Diff | 差异 | The line-by-line changes between two versions. |
| HEAD | HEAD | The commit you are currently on. |
| Revert | 反转提交 | A new commit that undoes an earlier one. |