Discarding changes in Git is a common task when you want to undo changes, remove local changes, or reset your repository to a clean state.
In Git, you can discard changes in multiple ways depending on where they exist — whether they are unstaged, staged, or already committed. Common commands used include git restore,
git reset,
git clean, and
git stash.
In this guide, you will learn:
- How to discard changes in Git
- How to discard local changes safely
- How to remove all changes in Git
- How to undo changes using different Git commands
This tutorial covers real-world scenarios and best practices to help you safely discard changes without losing important work.
Git Discard Changes - Quick Cheat Sheet
| Task | Command |
|---|---|
| Discard unstaged changes (all files) | git restore . |
| Discard unstaged changes (specific file) | git restore <file> |
| Discard staged changes (unstage file) | git restore --staged <file> |
| Unstage all changes | git restore --staged . |
| Discard all local changes (staged + unstaged) | git reset --hard HEAD |
| Remove untracked files | git clean -f |
| Remove untracked files and directories | git clean -fd |
| Preview files to be deleted (safe mode) | git clean -n |
| Temporarily save changes (safe discard) | git stash |
| Restore stashed changes | git stash apply |
| Drop stashed changes permanently | git stash drop |
| Undo last commit but keep changes | git reset --soft HEAD~1 |
| Undo last commit and unstage changes | git reset --mixed HEAD~1 |
| Undo last commit and delete changes | git reset --hard HEAD~1 |
| Revert a committed change safely | git revert <commit> |
| Discard changes in a file from last commit | git restore --source=HEAD <file> |
| Discard changes from a specific commit | git restore --source=<commit> <file> |
| Remove all changes and reset repo | git reset --hard && git clean -fd |
What Does "Discard Changes" Mean in Git?
In Git, discarding changes means removing modifications that you no longer want to keep in your repository. These changes can exist in different stages such as the working directory (unstaged), staging area (staged), or even committed history.
Depending on where the changes exist, Git provides different commands like git restore, git reset, git clean, and git revert to safely or permanently discard them.
Discarding changes in Git means undoing modifications from working directory, staging area, or commit history using appropriate commands.
Working directory vs staging vs commits
To properly discard changes, you must understand Git’s three main states:
Working Directory (Unstaged Changes)
Files you modified but haven’t added usinggit add
→ Use:git restoreStaging Area (Index / Staged Changes)
Files added usinggit addbut not committed usinggit commit
→ Use:git restore --stagedorgit resetCommit History (Committed Changes)
Changes already saved in Git history usinggit commit
→ Use:git revertorgit reset
When you should discard changes vs keep them
You should discard changes when:
- Changes are experimental or incorrect
- You want to revert to last working version
- You accidentally modified or staged files
- You want to clean your working directory
You should NOT discard changes when:
- Changes are important but incomplete → use
git stash - You are unsure → take backup or stash first
- Changes are already pushed → use
git revertinstead of reset
Git Discard vs Remove vs Undo Changes
In Git, different users use different terms for the same action:
- Discard changes in Git → Remove unwanted modifications
- Remove changes in Git → Delete local updates
- Undo changes in Git → Revert to previous state
- Delete changes in Git → Permanently remove modifications
All these actions can be performed using commands like
git resetgit cleangit revertgit restore
Understanding the Git Discard Workflow

The above workflow diagram simplifies how to decide which Git command to use when discarding changes:
Start with unwanted changes
- Files you modified (after using
git addor before committing withgit commit) and now want to discard them
- Files you modified (after using
Check if changes are staged
If No (unstaged changes):
- Use:
git restore .→ discard all changesgit restore <file>→ discard specific filegit clean -f→ remove untracked files
- Use:
If Yes (staged changes):
- Use:
git restore --staged <file>→ unstage changesgit reset --hard→ discard everything (dangerous)
- Use:
If changes are already committed
- Use:
git revert <commit>→ safe undogit reset --hard→ remove commits (use carefully)git stash→ temporarily save changes
- Use:
Key Insight:
The correct command depends on where your changes exist (unstaged, staged, or committed). Using the wrong command can lead to permanent data loss, especially with git reset --hard.
How to Discard Unstaged Changes in Git
Unstaged changes are modifications in your working directory that have not been added using git add.
Discard changes in a specific file (git restore )
To discard changes in a single file:
git restore app.pyThis will:
- Restore the file to its last committed state
- Remove all local modifications
Quick Tip:
Use this when you made unwanted edits to a file and want to revert it.
Discard all local changes (git restore .)
To discard all unstaged changes in the repository:
git restore .This will:
- Reset all modified files
- Keep staged and committed changes untouched
Discard Staged Changes (Unstage Changes)
Staged changes are files added using
git add and ready to be committed.
Remove file from staging area (git restore --staged)
To unstage a specific file:
git restore --staged app.pyThis will:
- Move the file from staging area → working directory
- Keep your changes intact
Unstage all changes safely
To unstage all files:
git restore --staged .Alternative (older command):
git resetHow to Discard All Local Changes in Git (Reset Everything)
This is used when you want to completely clean your working directory.
Use git reset --hard HEAD
git reset --hard HEADThis will:
- Remove all staged and unstaged changes
- Reset repository to last commit
Difference between reset --soft, --mixed, --hard
| Command | What it does |
|---|---|
git reset --soft | Moves HEAD, keeps staged + working changes |
git reset --mixed | Unstages changes, keeps working directory |
git reset --hard | Deletes all changes (working + staged) |
Quick Insight:
- Use
--soft→ undo commit only - Use
--mixed→ unstage changes - Use
--hard→ delete everything
How to Remove Untracked Files in Git (git clean)
Untracked files are files not added to Git (not part of repo).
Use git clean -f and -fd
Remove untracked files using
git clean
git clean -fRemove untracked files and directories:
git clean -fdSafe dry run using git clean -n
Before deleting files, always preview:
git clean -nThis shows:
- Which files will be removed
- Helps avoid accidental deletion
Best Practice:
Always run git clean -n before git clean -f
Temporarily Discard Changes (Safe Method)
Sometimes you don’t want to permanently delete changes — instead, save them temporarily using
git stash—you just want to set them aside temporarily.
Use git stash to save changes
You can use git stash:
git stashThis will:
- Save both staged and unstaged changes
- Clean your working directory
Add a message:
git stash save "WIP changes"Restore stashed changes later
View stashes:
git stash listApply stash:
git stash applyApply and remove stash:
git stash popDelete stash:
git stash dropBest Use Case:
When switching branches or testing something without losing work.
Undo Already Committed Changes
When changes are already committed, you must use different approaches.
Use git revert (safe for shared repos)
git revert <commit>This will:
- Create a new commit that undoes previous changes
- Preserve Git history (safe for teams)
Recommended for:
Shared repositories and pushed commits
Use git reset (dangerous but powerful)
git reset --hard HEAD~1This will:
- Remove the last commit completely
- Delete associated changes
Discard Changes for a Single File Only
Useful when you want to discard changes in just one file instead of the entire repo.
Restore file to last commit
git restore app.pyThis will:
- Discard all changes in the file
- Restore from last commit (HEAD)
Restore file from specific commit
git restore --source=<commit> app.pyExample:
git restore --source=HEAD~1 app.pyThis helps:
- Recover older file versions
- Compare and selectively restore
Common Mistakes While Discarding Changes (High Ranking Section)
Difference between restore, reset, checkout confusion
| Command | Purpose |
|---|---|
git restore | Discard changes (recommended modern command) |
git reset | Unstage or reset commits |
git checkout | Older method (now replaced by restore/switch) |
Key Tip:
Use git restore for discarding changes instead of checkout.
Why changes are not getting discarded
Common reasons:
- Changes are already staged → use
--staged - Changes are committed → use
resetorrevert - File is untracked → use
git clean - Wrong command used for the state
Check current state using
git status:
git statusRisk of git reset --hard (data loss)
git reset --hardThis will:
- Delete all local changes permanently
- Cannot be undone easily
Safer alternative:
git stashGolden Rule:
If unsure → stash first, then proceed
Frequently Asked Questions
1. How to discard changes in Git?
You can discard changes in Git using commands likegit restore, git reset, git checkout, or git clean depending on whether changes are unstaged, staged, or untracked.2. How to discard local changes in Git?
Usegit restore . to discard all local changes in the working directory or git restore <file> to discard changes in a specific file.3. How to discard all changes in Git?
Usegit reset --hard HEAD to discard all staged and unstaged changes. Be careful, as this will permanently remove all local modifications.4. How to discard unstaged changes in Git?
Usegit restore <file> or git restore . to discard unstaged changes in files.5. How to discard staged changes in Git?
Usegit reset <file> or git restore --staged <file> to unstage changes.6. How to delete untracked files in Git?
Usegit clean -f to remove untracked files and git clean -fd to remove untracked directories as well.7. What is the safest way to undo changes in Git?
The safest way is usinggit stash to temporarily save changes or git revert to undo committed changes without rewriting history.8. What is the difference between git reset and git restore?
git reset is used to unstage changes or reset commits, while git restore is used to discard changes in the working directory or staging area in a safer and more intuitive way.Summary
Discarding changes in Git depends on where your changes exist in the workflow involving
git add,
git commit, and cleanup commands like
git clean
- Unstaged changes →
git restore - Staged changes →
git restore --stagedorgit reset - Untracked files →
git clean - Committed changes →
git revertorgit reset - Temporary discard →
git stash
Understanding these scenarios helps you choose the right command and avoid data loss.




![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)
![[SOLVED] Pulling is not possible because you have unmerged files](/pulling-is-not-possible-because-you-have-unmerged-files/git-pull-conflict_hu_849287d4525bf4c8.webp)




