Discarding Local Modifications
Strategies for Canceling Local Changes
Discarding Changes in Specific Files
graph LR
A[Modified File] --> B{Discard Method}
B --> |checkout| C[Restore Original State]
B --> |restore| D[Remove Local Changes]
Using git checkout
Restore a single file to its last committed state:
## Discard changes in specific file
git checkout -- filename.txt
## Discard changes in multiple files
git checkout -- file1.txt file2.txt
Using git restore
Modern Git method for discarding local modifications:
## Discard changes in specific file
git restore filename.txt
## Discard changes in multiple files
git restore file1.txt file2.txt
Discarding All Local Changes
Command |
Scope |
Effect |
git checkout . |
All tracked files |
Reverts entire working directory |
git restore . |
All tracked files |
Removes all local modifications |
Advanced Discard Techniques
Cleaning Untracked Files
## Remove untracked files
git clean -f
## Remove untracked files and directories
git clean -fd
Best Practices
- Always verify changes before discarding
- Use
git status
to understand current modifications
- Be cautious with global discard commands
LabEx recommends careful review before executing discard operations to prevent accidental data loss.