AI Hints
Home›Git & GitHub Tutorial›Cleaning Up the Folder
Git & GitHub · Lesson 38 of 144

Practice: Undo Challenges

Choose the right undo.

⏱ 2 min read▶ Video at 40:59🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

You now know many ways to undo things. The hard part is choosing the right one. Here are four situations. For each one, pause, and decide which command you'd use.

Practice: Undo Challenges
From the video: Practice: Undo Challenges
  • Many ways to undo
  • Choosing is the hard part
  • Four situations

Which undo?

Situation one. You edited a file, and want the last committed version back. Use git restore. Situation two. You staged a file by mistake. Git restore dash dash staged. Situation three. Your last commit has a typo, and isn't pushed. Git commit dash dash amend. Situation four. A pushed commit broke main. Git revert.

SituationCommand
Undo edits to a filegit restore <file>
Unstage a filegit restore --staged <file>
Fix the last commit, not pushedgit commit --amend
Undo a pushed commitgit revert <hash>

Situation four, for real

Let's do situation four for real. The latest commit, broken layout, made the page invisible. It's already on GitHub, so we revert it. Git creates the undo commit, and we push it. Main is fixed, and nobody's history was rewritten.

git
git log --oneline -n 2
e8cffdc (HEAD -> main, origin/main) Broken layout
f63a17a Link to about page
git revert --no-edit HEAD
[main 7fbe649] Revert "Broken layout"
 Date: Mon Jan 5 09:41:06 2026 +0000
 1 file changed, 1 deletion(-)
git push
To https://github.com/sam/my-site.git
   e8cffdc..7fbe649  main -> main

Summary

Restore for edits, restore staged for staging, amend for the last private commit, revert for shared commits. Next, the most powerful idea in Git. Branches.

Key takeaways
  • Edits: restore
  • Last private commit: amend
  • Shared commit: revert
Next up: What a Branch Is — a separate line of work