Course contentsShow
← Computer Science
Lesson 2178 of 2,87249. Version Control with GitFree lesson

Undoing Changes in Working Directory

Discard local modifications with 'git checkout' or 'git restore' to revert files to last commit.

Undoing Changes in Working Directory

You'll learn how to discard uncommitted local modifications and restore files to their last committed state using Git.

The Core Idea

Sometimes you edit a file and realize you want to throw away your changes and start fresh from the last commit. Git provides commands to undo modifications in your working directory—essentially saying "forget what I just did to this file."

Think of it like having a "revert to saved" button in a video game. You've been playing around since your last save point, but things went wrong. Instead of manually undoing every action, you just reload from that save.

Two Commands, Same Goal

Modern Git (git restore)
The newer, more explicit command:

git restore myfile.txt

This discards changes in myfile.txt and restores it to match the last commit.

Legacy approach (git checkout)
The older method that still works:

git checkout -- myfile.txt

The -- separates the command from the filename, preventing ambiguity.

When to Use This

  • You experimented with code that didn't work
  • You accidentally modified a file
  • You want to compare a fresh version against your changes (restore to a different file, then compare)

Important Warning

This operation is destructive and irreversible. Your uncommitted changes are permanently lost—they're not stored anywhere in Git's history. Only use this when you're certain you want to discard your work.

Key Takeaway: Use git restore <file> (or git checkout -- <file>) to discard working directory changes and revert files to their last committed state, but remember this permanently deletes uncommitted modifications.