Discard Git Repo Changes via Terminal
Safely discard Git changes and untracked files directly from Terminal.
To safely discard untracked files from your Git repository using Terminal, start with a dry run to preview deletions:
1
git clean -n # Preview files to be removed
Execute the commands if you’re certain:
1
2
3
git clean -f # Remove untracked files only
git clean -fd # Remove untracked files and directories
git clean -i # Interactive mode for selective removal
To revert changes and restore your files to their last committed state:
1
2
3
4
git checkout -- . # Discard unstaged changes only
git reset --hard HEAD # Reset all staged and unstaged changes
git restore . # Modern alternative (Git 2.23+)
git reset HEAD # Unstage changes without losing edits
Always verify the repository status before making destructive changes:
1
git status # Review current repository state
These commands can permanently delete data, so test carefully or use git stash
if you might need to recover changes.
Efficiently maintain your repositories with these essential Git commands!
☕ Support My Work
If you found this post helpful and want to support more content like this, you can buy me a coffee!
Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏