Sometimes in development, things go sideways — maybe your code isn’t working as expected, or you just want to start over. Fortunately, Git gives you several ways to remove all changes and return to a clean state, depending on the type of changes you’ve made.
In this guide, we’ll explore how to discard uncommitted, staged, and committed changes in Git and revert to a stable state.
🗂️ Types of Changes in Git
Before we start, let’s define the three common states of changes in Git:
- Unstaged changes – Modified files not yet added with
git add
. - Staged changes – Files added to the staging area with
git add
. - Committed changes – Changes saved in the local repository with
git commit
.
🛠️ 1. Discard All Uncommitted Changes (Unstaged + Staged)
To remove all uncommitted changes and return to the last commit:
git reset --hard
This command resets the working directory and staging area to the last committed state.
⚠️ Warning: This will erase all unsaved work — make sure you want to lose these changes.
🧹 2. Clean Untracked Files (e.g., new files, logs)
To delete untracked files and directories (not in Git):
git clean -fd
-f
: force delete-d
: include directories
Example: removes files like
temp.txt
or folders likenode_modules/
if not committed.
🔁 3. Undo Last Commit (If You Want to Keep the Changes)
If you’ve committed but want to undo the commit while keeping the changes in your working directory:
git reset --soft HEAD~1
--soft
: removes the last commit but retains the changes staged.
🔄 4. Undo Last Commit and Changes
To undo the last commit and discard all changes made in it:
git reset --hard HEAD~1
🔃 5. Reset to a Specific Commit (Wipe All Changes After)
If you want to roll back to an earlier commit and discard everything after:
git reset --hard <commit-hash>
Find the commit hash with:
git log --oneline
Example:
git reset --hard a1b2c3d
💣 6. Remove Everything and Reclone (Ultimate Reset)
If your repository is beyond repair, the most extreme solution is:
rm -rf your-repo-folder
git clone https://github.com/your-user/your-repo.git
🧠 Summary Cheat Sheet
Action | Command |
---|---|
Discard uncommitted changes | git reset --hard |
Remove untracked files | git clean -fd |
Undo last commit (keep changes) | git reset --soft HEAD~1 |
Undo last commit (discard changes) | git reset --hard HEAD~1 |
Reset to specific commit | git reset --hard <hash> |
🏁 Conclusion
Whether you’ve made a few changes or a series of commits you want to roll back, Git provides multiple levels of control to help you reset your project. Just be sure you understand each command’s effect — especially the --hard
and clean
options, which permanently delete changes.