discard
git
unstaged

How do I discard unstaged changes in Git?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Discarding unstaged changes in Git means restoring files in your working tree back to a known committed state. The main danger is simple: the operation is destructive, so you should be certain the changes are not worth keeping before you run it.

The modern Git command for this is usually git restore. Older answers often use git checkout --, which still works in many environments but is no longer the clearest tool for the job.

Check What Will Be Lost First

Before discarding anything, inspect the working tree:

bash
git status
git diff

That tells you:

  • which files are modified but unstaged
  • whether there are staged changes too
  • whether untracked files are also present

This matters because discard unstaged changes and remove untracked files are different operations.

Discard All Unstaged Changes

To restore all tracked files in the current directory tree to HEAD:

bash
git restore .

That resets the working tree copy of tracked files and discards unstaged modifications.

If you want to be more explicit:

bash
git restore --worktree .

For most day-to-day use, the shorter form is enough.

Discard Changes in One File

To restore one specific file:

bash
git restore path/to/file.txt

This is safer than restoring the entire tree when only one file should be reset.

It is the usual answer when you made an experiment in one file and want to throw it away without affecting anything else.

Older Equivalent: git checkout --

Older Git advice often uses:

bash
git checkout -- path/to/file.txt

or:

bash
git checkout -- .

These commands can still work, but git restore was introduced to separate restore file contents from switch branches. That makes intent clearer and reduces confusion.

In new examples, prefer git restore.

Staged Changes Are Different

If a file is already staged, git restore path/to/file.txt only restores the working tree by default. It does not automatically unstage the index copy.

To unstage and restore both index and working tree for a file, you would use:

bash
git restore --staged --worktree path/to/file.txt

That is important because many users say discard changes when they actually mean:

  • remove unstaged edits
  • or remove staged edits too

Git treats those as different states.

Untracked Files Are Not Included

git restore only affects tracked files. If you also want to remove untracked files, that is a separate command:

bash
git clean -fd

Be careful with this. git clean permanently removes untracked files and directories.

A safe preview is:

bash
git clean -nd

That shows what would be removed without actually deleting anything.

If You Are Unsure, Stash Instead

If you are not completely sure you want to lose the changes, stash them first:

bash
git stash push -u -m "temporary backup before restore"

Then, if you still want to discard the working tree:

bash
git restore .

This is a good habit when the change set is messy and you are not fully confident that nothing valuable is hiding there.

Common Pitfalls

  • Running a destructive restore without checking git diff first.
  • Forgetting that git restore affects tracked files only, not untracked files.
  • Using git restore when the real issue is a staged change that also needs --staged.
  • Confusing git checkout -- branch switching with file restoration.
  • Using git clean -fd casually and deleting untracked work unintentionally.

Summary

  • Use git restore . to discard unstaged changes in tracked files across the working tree.
  • Use git restore path/to/file to discard changes in one specific file.
  • Prefer git restore over older git checkout -- examples in modern Git.
  • Use git clean separately for untracked files, and preview with -n first.
  • If you are unsure, stash before discarding so the changes are recoverable.

Course illustration
Course illustration

All Rights Reserved.