Undoing Changes in Git
Even experienced developers make mistakes — committing wrong files, breaking builds, or pushing changes too early. Fortunately, Git gives you multiple ways to undo, fix, and recover from almost any situation.
In this tutorial, you’ll learn the safest and most powerful ways to undo changes in Git — from local edits to public commits.
Git’s Safety Principle
Before jumping in, it’s important to understand:
Git rarely deletes anything permanently. It keeps a complete history of your commits — even after undo operations.
You can always recover past states using git reflog.
1. Discarding Local Changes (Unstaged)
If you’ve modified a file but haven’t staged it yet (git add not used), you can discard those changes:
git restore <filename>
Example:
git restore index.html
This command reverts the file to the last committed version.
2. Unstaging Files
If you’ve already staged a file with git add, but want to unstage it:
git restore --staged <filename>
Example:
git restore --staged app.js
This moves the file out of the staging area but keeps your changes in the working directory.
3. Undoing the Last Commit (Without Losing Work)
Made a commit too soon? You can “uncommit” it while keeping your changes intact:
git reset --soft HEAD~1
--soft: Removes the commit, keeps changes staged.HEAD~1: Refers to one commit before the currentHEAD.
Use this if you forgot to add a file or need to rewrite your message.
4. Undoing the Last Commit (Discarding Changes)
If you want to completely discard the last commit and changes:
git reset --hard HEAD~1
Warning: This permanently deletes uncommitted changes. Use this only if you’re absolutely sure you don’t need the changes.
5. Reverting a Commit (Safe for Public Repos)
When a commit is already pushed to GitHub, don’t use reset.
Instead, create a new commit that “undoes” the previous one:
git revert <commit-hash>
Example:
git revert a1b2c3d
This creates a new commit reversing the changes — perfect for shared projects.
6. Stashing Temporary Work
You’re in the middle of coding, but need to switch branches quickly? Save your current progress temporarily using stash.
git stash
Switch branches, then reapply your work:
git stash pop
Useful stash commands:
| Command | Description |
|---|---|
git stash list | Show all stashed changes |
git stash apply | Apply last stash without deleting it |
git stash clear | Delete all stashed items |
7. Viewing Differences Before Undoing
Always review your changes before undoing anything.
git diff
Shows unstaged changes.
git diff --staged
Shows staged changes ready for commit. This helps confirm what you’re about to remove or reset.
8. Recovering Lost Commits with Reflog
If you’ve reset or deleted something by mistake — don’t panic. Use git reflog to view the history of HEAD changes:
git reflog
Find your lost commit hash and restore it:
git checkout <commit-hash>
or move your branch back:
git reset --hard <commit-hash>
Git Reflog is your time machine — use it to recover almost anything.
Common Scenarios & Solutions
| Problem | Command | Safe to Use |
|---|---|---|
| Undo unstaged edits | git restore <file> | ✅ Yes |
| Unstage a file | git restore --staged <file> | ✅ Yes |
| Undo last commit, keep changes | git reset --soft HEAD~1 | ✅ Yes |
| Undo last commit, discard changes | git reset --hard HEAD~1 | ⚠️ No (Deletes work) |
| Undo a pushed commit | git revert <commit-hash> | ✅ Yes |
| Save unfinished work | git stash | ✅ Yes |
Best Practices
- Always commit frequently with meaningful messages.
- Use
git diffbefore resetting or reverting. - Use
revertfor shared branches (safe for others). - Backup or stash changes before destructive actions.
- Use
git reflogwhen you’ve lost commits.
Next Up
Now that you know how to undo and recover safely, move on to mastering team collaboration: 👉 Advanced Git Concepts
Additional Resources
- Git Official Docs: Undoing Things
- Atlassian Git Tutorials
- Learn Git Branching (Interactive)
- Git Cheatsheet (GitHub)
Summary
| Command | Purpose |
|---|---|
git restore | Discard local changes |
git restore --staged | Unstage changes |
git reset | Move commit pointers |
git revert | Safely undo commits |
git stash | Temporarily save work |
git reflog | Recover lost commits |
💙 This guide is part of the CodeHarborHub Git & GitHub learning path — helping developers debug faster, recover smarter, and collaborate confidently.