How do I discard unstaged changes in Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
That resets the working tree copy of tracked files and discards unstaged modifications.
If you want to be more explicit:
For most day-to-day use, the shorter form is enough.
Discard Changes in One File
To restore one specific file:
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:
or:
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:
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:
Be careful with this. git clean permanently removes untracked files and directories.
A safe preview is:
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:
Then, if you still want to discard the working tree:
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 difffirst. - Forgetting that
git restoreaffects tracked files only, not untracked files. - Using
git restorewhen the real issue is a staged change that also needs--staged. - Confusing
git checkout --branch switching with file restoration. - Using
git clean -fdcasually 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/fileto discard changes in one specific file. - Prefer
git restoreover oldergit checkout --examples in modern Git. - Use
git cleanseparately for untracked files, and preview with-nfirst. - If you are unsure, stash before discarding so the changes are recoverable.
Related reading
- How do I find and restore a deleted file in a Git repository?
- How do I get the current branch name in Git?
- How do I make git use the editor of my choice for editing commit messages?
- How do I modify a specific commit?
- How do I push a new local branch to a remote Git repository and track it too?
- How do I remove local (untracked) files from the current Git working tree?
- How do I resolve merge conflicts in a Git repository?
- How do I squash my last N commits together?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.