How do I discard unstaged changes in Git?
How do you discard unstaged changes in Git? What commands can you use to safely remove modifications and restore files to their last committed state?
When working with Git, it’s common to make edits you later decide not to keep. This raises the question: “How do I discard unstaged changes in Git?” Unstaged changes are modifications in your working directory that haven’t been added to the staging area yet. Fortunately, Git offers simple ways to remove them.
Discard Changes in a Single File
If you only want to discard changes in one file:
git checkout -- filename
Or, in modern Git versions:
git restore filename
This reverts the file back to its last committed version.
Discard All Unstaged Changes
To remove changes across your entire working directory:
git restore .
Or using the older command:
git checkout -- .
Discard Untracked Files Too
If you also want to delete new, untracked files (not yet staged or committed):
git clean -fd
Be careful—this permanently deletes untracked files and directories.
Safety Tip:
Always double-check before discarding, as these changes cannot be recovered unless backed up or stashed. If you’re unsure, consider using:
git stash
This temporarily saves your changes without committing them.
Key Takeaway:
Use git restore (or git checkout -- in older versions) to discard unstaged changes safely. For complete cleanup, combine it with git clean—but use caution to avoid losing important files.