Version control systems like Git are essential tools for modern software development, offering robust tracking, collaboration, and history management. However, developers often face a common question: “How do I undo something in Git?”
Whether you committed too soon, staged the wrong file, or want to roll back to a previous state, Git provides powerful commands to undo changes safely and efficiently.
In this guide, we’ll walk through several common scenarios and how to undo changes using Git.
🔁 Levels of Undo in Git
Undoing changes in Git can happen at various levels:
- Undo changes in the working directory
- Unstage changes
- Amend a commit
- Undo a commit
- Revert a commit
- Reset to a previous state
Let’s look at each case with examples.
1. 📝 Undo Changes in the Working Directory
If you’ve modified a file but haven’t staged it yet, and want to discard those changes:
git checkout -- <filename>
Or, using the modern Git command:
git restore <filename>
Example:
git restore index.html
This will reset the file to the last committed version.
2. 📦 Unstage Changes
If you’ve added files to the staging area using git add
but haven’t committed yet:
git reset <filename>
Or:
git restore --staged <filename>
Example:
git restore --staged app.js
This will unstage the file but keep your local modifications.
3. ✏️ Amend the Last Commit
To modify the most recent commit message or include additional changes:
git commit --amend
This opens an editor to change the commit message or adds any new staged changes to the last commit.
Note: Use this only if the commit hasn’t been pushed to a shared branch.
4. ❌ Undo the Last Commit (Keep Changes)
You committed, but realized it was a mistake and want to undo the commit while keeping your changes:
git reset --soft HEAD~1
--soft
keeps all changes staged.HEAD~1
refers to the previous commit.
5. 🔄 Undo the Last Commit (Discard Commit & Unstage Changes)
If you want to keep the changes but remove the commit and unstage everything:
git reset --mixed HEAD~1
Your files remain in the working directory, but are no longer staged or committed.
6. 🧨 Undo the Last Commit (Erase Completely)
To completely remove the last commit and discard the changes:
git reset --hard HEAD~1
⚠️ Warning: This deletes changes permanently. Use with caution.
7. 🪃 Revert a Commit (Safe for Shared Repos)
To undo the effects of a specific commit without rewriting history:
git revert <commit-hash>
This creates a new commit that negates the changes from the given commit.
Example:
git revert a1b2c3d4
This is safe to use on branches others are working on.
8. 🔍 View History to Identify Commits
Not sure which commit to undo? Use:
git log --oneline
or for a graphical view:
git log --graph --oneline --all
This helps identify the commit hash you want to target.
🛟 Bonus: Create a Backup Before Undoing
If you’re unsure about resetting or reverting, consider creating a backup branch first:
git branch backup-before-reset
Now you can safely experiment with undo commands and return to your backup if needed.
🧠 Final Thoughts
Undoing changes in Git is a powerful skill that every developer should master. Whether you’re fixing mistakes or restructuring your history, Git offers flexible tools to recover your work safely.
Always remember:
- Use
revert
for safe public history changes. - Use
reset
with care—especially--hard
. - Create backups when in doubt.
With the right approach, you’ll spend less time worrying about mistakes and more time writing great code.