ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Git Version Control Tutorial• 7 min read

Undoing Mistakes: git restore, reset, revert & reflog

Soft/Mixed/Hard resets, non-destructive git revert, and emergency reflog recovery

Git provides precise surgical tools to discard unstaged edits, unstage files, undo local commits, or safely invert shared public commits.

Reset vs Revert vs Reflog

git restore <file>: Discards uncommitted working changes in a file.

git reset --soft HEAD~1: Undoes the last commit but keeps all changes staged.

git reset --hard HEAD~1: Permanently discards the last commit and wipes working changes.

git revert <commit-sha>: The SAFE way for public branches! Creates a new commit that applies the exact inverse diff of the target commit without rewriting history.

git reflog: The emergency safety net. Logs every movement of HEAD. Even if you accidentally run git reset --hard, you can recover lost commits via reflog!

Rescuing Lost Commits via Git Reflog
# Accidentally wiped commits with hard reset:
git reset --hard HEAD~3

# 1. View Git internal audit trail
git reflog
# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: critical feature completed

# 2. Resurrect the lost commit!
git reset --hard HEAD@{1}
git reflog tracks every HEAD movement, making accidental data loss almost impossible.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which Git command safely undoes a commit on a shared public branch by creating a brand-new commit that applies the inverse diff?