Discarding Uncommitted Changes

How to use `git restore` to safely unstage files or permanently discard local edits.

Version Control5 min readConcept 18 of 29

The git restore command

git restore is a command used to restore files in your working directory or staging area to a previous state.

Introduced in Git 2.23 (alongside git switch), it specifically handles modifying file contents. It replaces the older, confusing git checkout -- <file> command, giving developers a precise tool to undo local changes.

Why it matters

Web development involves rapid iteration—tweaking CSS, testing logic, breaking layouts. It is incredibly common to experiment with a file and realize you want to completely undo your work.

git restore gives you a scalpel to quickly discard those local edits, or to unstage a file you accidentally added, without touching your commit history.

How it works (Two Modes)

**Mode 1 (Discard):** Running git restore <file> copies the version of the file from the Staging Area and overwrites the Working Directory. Your local edits are destroyed.

**Mode 2 (Unstage):** Running git restore --staged <file> copies the version of the file from the Last Commit (HEAD) and overwrites the Staging Area. Crucially, your working directory edits remain completely intact.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1You've been experimenting with `styles.css` but don't like the changes and haven't staged them yet. What is the danger of running `git restore styles.css`?
  2. 2You typed `git add .` and accidentally staged a sensitive file. You want to remove it from the staging area but KEEP the file and your modifications in your folder. Which command should you use?
  3. 3Why did Git introduce `git restore` and `git switch` in version 2.23?

Remember this

  • git restore <file> permanently discards unstaged local changes.
  • git restore --staged <file> safely unstages a file without losing work.
  • Think of it as copying a file from the past to overwrite the present.

Done with this concept?

Mark it complete to track your progress. No login needed.