AI Hints
Home›Git & GitHub Tutorial›Undoing Changes
Git & GitHub · Lesson 24 of 144

Throwing Away Changes

Git restore.

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

Here's where Git really pays off. You've been editing, and everything went wrong. You just want the file back the way it was at the last commit. One command does it.

Throwing Away Changes
From the video: Throwing Away Changes
  • Your edits went wrong
  • You want the last saved version
  • One command

git restore

We made a mess of the home page. Git diff shows the bad line we added. To throw away our edits, we type git restore, and the file name. Now diff shows nothing, and status says the working tree is clean. The file is exactly as it was in the last commit.

git
git diff
diff --git a/index.html b/index.html
index 93dd595..1cf91c4 100644
--- a/index.html
+++ b/index.html
@@ -1,3 +1,4 @@
 <h1>My Site</h1>
 <p>Welcome!</p>
+<p>oops, I broke everything</p>
 <a href="about.html">About</a>
git restore index.html
git diff
git status
On branch main
nothing to commit, working tree clean

Be careful

One warning. Git restore really throws your edits away. They were never committed, so Git never saved them, and they can't come back. Before you restore, glance at git diff, and be sure you don't want those changes.

Restorediscards your edits
Never committednever saved
No undofor uncommitted edits
Check firstgit diff

Summary

git restore puts a file back the way it was at the last commit, and throws away your edits for good. Next, how to unstage something.

Key takeaways
  • git restore <file>
  • Back to the last commit
  • Uncommitted edits are lost
Next up: Unstaging a File — git restore --staged