To revert all local changes in a Git project to a previous state, use one of these commands depending on your specific needs:
1. Discard Unstaged Changes (Not Added to Staging)
git restore . # For all files (Git v2.23+)
git checkout -- . # Alternative for older Git versions
2. Discard Staged Changes (Added to Staging)
git reset --hard # Unstage and discard all changes
3. Revert to Last Commit (Discard All Uncommitted Changes)
git reset --hard HEAD # Reset to last commit (destructive - use with caution!)
4. Revert to a Specific Commit (Discard All Newer Commits)
git reset --hard <commit-hash> # e.g., git reset --hard a1b2c3d
5. Remove Untracked Files/Directories
git clean -df # Delete untracked files and directories (-f force, -d directories)
6. Full Cleanup (All Changes + Untracked Files)
git reset --hard && git clean -df # Reset + delete untracked files
7. Safe Alternative: Stash Changes
If you want to save changes for later instead of deleting them:
git stash -u # Stash tracked + untracked files
git stash drop # Delete the stash later (optional)
Key Notes
git reset --hard
is destructive and cannot be undone. Use cautiously!git clean
deletes untracked files permanently. Add-n
for a dry-run first.- To revert a specific file:
git restore path/to/file # Discard changes to a single file
Example Workflow
# 1. Check status
git status
# 2. Discard all local changes (tracked/untracked)
git reset --hard && git clean -df
# 3. Verify
git status # Should show "nothing to commit"
Choose the method that matches your needs, and always double-check with git status
before running destructive commands!