git/How To/Discard Changes to Working Dir
Properties
- tags
- Diataxis/How To, Git, Git Commands
How to Discard Changes to Working Directory with Git
Overview
- This guide shows you how to safely discard unwanted changes in your Git working directory
- Useful when you want to abandon local modifications and return to a known good state
Prerequisites
- Git installed on your system
- A Git repository with changes you want to discard
- Basic understanding of Git staging area and commits
Steps
1. Review Changes to be Discarded
- Run
git status to see what changes will be discarded - Make sure you really want to discard these changes (they cannot be recovered!)
2. Choose Your Discard Method
Option A: Discard All Changes (Staged and Unstaged) - git/reset/--hard
- Run
git reset --hard HEAD - This discards all changes and returns to the last commit
Option B: Discard Only Unstaged Changes - git/checkout
- For all unstaged changes:
- For specific files:
- Run
git checkout -- path/to/file
Option C: Discard Only Staged Changes - git/reset
- First unstage the changes:
- Then follow Option B if you want to discard them completely
Option D: Discard Untracked Files - git/clean
Preview what will be removed (STRONGLY RECOMMENDED)
- Run
git clean -n git/clean/--dry-run - This shows which files would be deleted without actually deleting them
Remove untracked files
- Run
git clean -f git/clean/--force to remove all untracked files - For specific directories:
git clean -f <directory>
Remove untracked files and directories
Remove ignored files too
Safety Measures
Before Discarding
- Create a backup branch if unsure:
- Run
git branch backup-YYYY-MM-DD
- Or stash your changes:
- Run
git stash save "description of changes"
- For untracked files:
- ALWAYS use
git clean -n first to preview what will be deleted - Consider moving important untracked files to a different location
- Check your
.gitignore to ensure you won't delete needed files
Troubleshooting
Common Issues
- If you accidentally discarded changes:
- Check
git reflog for recovery options - Restore from stash if you created one:
git stash pop
- If some files aren't being discarded:
- Check if they're being tracked by Git
- For untracked files, use
git clean -f
- If
git clean isn't removing directories:- Add the
-d flag: git clean -fd
- If
git clean shows "fatal: clean.requireForce defaults to true":- Use the
-f (force) flag as shown above - Or configure git:
git config clean.requireForce false
Related