Undoing Changes: Restore and Reset Basics
You'll learn how to safely discard unwanted changes and unstage files before committing them.
Even experienced developers make mistakes or change their minds. Git provides ways to undo work you've done in your working directory or staging area—before you commit. Think of this as erasing pencil marks before you write in pen.
Discarding Changes in Your Working Directory
If you've modified a file but haven't staged it yet, and you want to throw away those changes completely, use:
git restore <filename>
This resets the file back to what it looked like in your last commit. Warning: The changes are gone forever—there's no undo for this!
Analogy: Imagine you're editing a draft on paper. git restore is like crumpling up your edits and pulling out the original clean copy from your folder.
Unstaging Files
Sometimes you've staged a file with git add but realize you don't want it in the next commit. To remove it from the staging area (but keep your changes in the working directory):
git restore --staged <filename>
This moves the file back to "modified but not staged" status. Your actual changes remain intact—you're just saying "not ready to commit this yet."
Checking First
Always run git status before undoing anything. It shows what state your files are in and often suggests the exact commands you need.
Basic Reset Preview
The git reset command can also unstage files (though git restore --staged is clearer). We'll explore reset more deeply later, but for now, know it exists for more complex undo scenarios.
Key Takeaway: Use git restore <file> to discard working changes permanently, and git restore --staged <file> to unstage while keeping your edits—always check git status first to understand what you're undoing.