β»οΈ 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 restore
to undo file changes (staged or unstaged) - Use
git reset
to undo commits (soft or hard depending on need) - Use
git revert
for safe undos in shared/pushed repositories - Use
git reflog
to 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 :