Git Undo: How to Undo Changes in Git (Beginner-Friendly Guide with Examples & FAQ)
Introduction: Git Undo
Made a mistake in Git? Donβt panic! Git provides powerful tools to undo almost anything β from local edits to commits and even pushed changes.
This guide explains how to undo:
- Local changes
- Staged files
- Commits
- Pushed commits
Why You Need Git Undo Options
| Scenario | Git Undo Tool to Use |
|---|---|
| Undo uncommitted file changes | git restore, git checkout |
| Unstage a file | git restore --staged |
| Undo a commit (keep changes) | git reset --soft |
| Undo a commit (remove changes) | git reset --hard |
| Revert a pushed commit safely | git revert |
| View history of changes | git log, git reflog |
Undoing Local File Changes
Undo Changes in Working Directory (Not Yet Staged)
git restore filename.txt
Restores the file to its last committed state.
Example:
git restore index.html
Undo Changes in All Files
git restore .
This restores all modified files in the current directory tree.
Undoing Staged Changes (Unstage a File)
Unstage a Specific File
git restore --staged filename.txt
Example:
git restore --staged login.js
The file stays modified but is removed from the staging area.
Unstage All Files
git restore --staged .
Undo Last Commit (Keep or Discard Changes)
Keep Changes (Undo Commit Only)
git reset --soft HEAD~1
Moves the last commit back to staging.
Discard Changes from Last Commit
git reset --hard HEAD~1
This removes the last commit and your code changes. Be careful!
Undo Multiple Commits
Undo the last N commits and discard all changes:
git reset --hard HEAD~3
Undo the last 3 commits and keep the changes as unstaged:
git reset --mixed HEAD~3
Undo Pushed Commits Safely
Use git revert (Preferred for Shared Repos)
git revert <commit-hash>
Example:
git revert abc1234
This creates a new commit that undoes the changes of the specified commit.
Undo Last Commit and Push (Dangerous!)
git reset --hard HEAD~1
git push --force
Only do this in your own branches. For shared repos, this may cause conflicts.
Viewing History to Undo Precisely
Show Commit History
git log
Show All Actions (Including Resets)
git reflog
Example Output:
7a8e2c3 HEAD@{0}: commit: Fixed login bug
d45f8e1 HEAD@{1}: reset: moving to HEAD~1
...
You can reset to any previous state:
git reset --hard HEAD@{1}
Summary β Git Undo
Git provides multiple ways to undo mistakes safely, based on your situation and workflow.
Key Takeaways:
- Use
git restoreto undo file changes (staged or unstaged) - Use
git resetto undo commits (soft or hard depending on need) - Use
git revertfor safe undos in shared/pushed repositories - Use
git reflogto view your complete action history
Real-World Relevance:
These commands are essential for:
- Debugging mistakes quickly
- Recovering accidentally deleted work
- Collaborating safely on shared codebases
Git Undo β Frequently Asked Questions
How do I undo a file edit before staging?
git restore filename.txt
This brings the file back to the last committed version.
How do I unstage a file?
git restore --staged filename.txt
The file will stay modified but is no longer staged.
How do I undo the last commit but keep changes?
git reset --soft HEAD~1
This removes the last commit but keeps your changes staged.
How do I undo changes permanently?
git reset --hard HEAD
This discards all unstaged and staged changes.
What is the difference between revert and reset?
| Command | Use Case | Safe for Team? |
|---|---|---|
reset | Rewrites commit history | Not safe |
revert | Creates a new commit that cancels a previous one | Yes |
How do I recover lost commits?
Use:
git reflog
Then:
git reset --hard HEAD@{n}
Replace n with the correct index from reflog output.
Share Now :
