Throwing Away Changes
Git restore.
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.

- 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 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.
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