Course contentsShow
← Git & GitHub
Lesson 26 of 962. Core Git MechanicsFree lesson

Discarding Unstaged Changes in the Working Directory

Learn to use the restore command to discard local modifications and revert files back to their last committed state.

In this lesson, you will learn how to safely revert your files to their last committed state when you have made local changes that you no longer want.

The "Oops" Moment

We have all been there: you start typing away in your project, experimenting with a new feature, only to realize halfway through that the code is headed in the wrong direction. You haven't committed these changes yet, and you haven't even added them to the staging area. You just want your file to go back to exactly how it looked in your last commit.

Using git restore

Git provides a powerful command for this specific scenario: git restore. Think of this command as your "undo" button for your working directory. When you run this command on a file, Git replaces the version in your working directory with the version currently held in your last commit.

If you have a file named script.js and you want to discard your unstaged edits, you simply run:

git restore script.js

Why use this instead of deleting?

You might be tempted to manually delete the lines you typed or re-type the original code, but that is prone to human error. Manual editing can leave behind invisible whitespace or stray characters. By using git restore, you are instructing Git to look at your "Source of Truth"—the last commit—and force the file in your working directory to match it perfectly.

Keep in mind that this is a destructive action for those specific changes; once you run this command, your local modifications that were not yet staged or committed are gone for good. It is a fantastic way to keep your working directory clean while you experiment.

If you ever find yourself in a state of "I just want to start over on this file," remember that your last commit is the safety net Git has built for you.

Key Takeaway: Use git restore <filename> to instantly discard unstaged local changes and revert a file to its last committed state.