# Discarding Uncommitted Changes

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

Subject: Version Control | Lesson 18 of 29 | 5 min read
Interactive lesson: https://codewithanimation.in/git/git-restore
Previous: [Tracking Branches](https://codewithanimation.in/git/tracking-branches.md) | Next: [Safely Undoing Commits](https://codewithanimation.in/git/git-revert.md)

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

## Common mistake: Permanent data loss

The #1 common mistake is forgetting that Git only protects what is committed.

Running `git restore <file>` on an unstaged file **permanently deletes your uncommitted work**. Because these edits were never committed to the repository, Git has no record of them. There is absolutely no way to recover the lost work.

## Tip: Copy and Paste from the Past

Mental Model: Think of `git restore` as copying a file from the past and pasting it over the present.

If you use `--staged`, you are pasting it into the Staging Area (unstaging). If you don't use flags, you are pasting it into the Working Directory (discarding).

## Try it: Choose what to restore

Compare restoring a working file with removing a staged change from the index.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-restore

## Check yourself

### Question 1

You'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`?

- A. It moves the changes into the staging area.
- B. It saves the changes in a temporary stash so you can retrieve them later.
- C. It permanently deletes your uncommitted changes to the file, and they cannot be recovered by Git.
- D. It reverts the file to the state of a different branch.

Correct answer: C

When you use `git restore <file>` on unstaged changes, Git overwrites your working directory file with the version from the index. Since the edits were never tracked in a commit, they are lost forever.

- Why not A: That is `git add`.
- Why not B: That is `git stash`.
- Why not D: It restores from the staging area or HEAD.

### Question 2

You 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?

- A. `git restore <file>`
- B. `git reset --hard HEAD`
- C. `git restore --staged <file>`
- D. `git checkout <file>`

Correct answer: C

The `--staged` flag tells Git to restore the Staging Area from the last commit (`HEAD`), effectively unstaging the file while leaving your Working Directory completely untouched.

- Why not A: This overwrites the working directory file, destroying your modifications.
- Why not B: This unstages the file but ALSO permanently deletes local modifications.
- Why not D: Like restore without flags, this destroys working directory edits.

### Question 3

Why did Git introduce `git restore` and `git switch` in version 2.23?

- A. Because `git revert` was causing too many merge conflicts.
- B. To replace `git reset` entirely.
- C. To split the overloaded `git checkout` command into two simpler commands: one for branches and one for files.
- D. To provide a way to recover deleted repositories.

Correct answer: C

Historically, `git checkout` was used for both switching branches and restoring files. This was confusing, so Git split its responsibilities into `git switch` (for branches) and `git restore` (for files) to make the interface safer and more intuitive.

- Why not A: `git revert` is for undoing full commits safely and is unrelated.
- Why not B: `git reset` is still necessary for moving branch pointers.
- Why not D: Git commands cannot restore a deleted repository folder.

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