Undoing Commits (git reset, git revert)
π‘ Intermediate
π Definition
Undoing commits in Git is handled by two distinct commands: git reset (which rewrites commit history by moving the active HEAD pointer backwards) and git revert (which safely undoes a past commit by creating a brand-new inverse commit without destroying history).
π Multilingual Explanation
English
git reset rewrites history by moving HEAD to a previous commit. Modes include --soft (keeps changes staged), --mixed (default, keeps changes unstaged in Working Directory), and --hard (discards all changes permanently). git revert <hash> is the safe choice for public/shared branches because it creates a new commit that undoes specified past changes without altering historical commits.
Hindi
git reset commit history ko peeche le jata hai. --soft changes ko staged rakhta hai, --mixed changes ko working directory mein rakhta hai, aur --hard sabhi changes ko permanently delete kar deta hai. Public GitHub branches ke liye git revert sabse safe hai kyunki yeh history delete kiye bina ek naya inverse commit banata hai.
Marathi
git reset dware commit history maghe ghetli jaate. --soft changes staged thevto, --mixed working directory madhye thevto, aani --hard sarv badal permanently kadhun takto. Remote GitHub branches sathi git revert vaparale pahije karan te navin commit tayar karun badal undo karte.
Hinglish
Unpublished local commits ke liye git reset use karein. Public/GitHub branches par kabhi bhi git reset --hard na chalayein; wahan git revert use karein. git revert ek naya commit add karta hai jo purane bug commit ke exact opposite changes apply karta hai.
π€ Why Do We Use Them?
If you committed broken code locally before pushing, git reset lets you uncommit and fix it. If a bug was pushed to a shared production branch days ago, git revert safely rolls back the bug without disrupting team history.
π§ Simple Explanation
git reset(Time Machine / Rewrite): Erasing pages from a history book as if those events never happened. Fine for your personal diary (local branch), but dangerous if others have already read those pages!git revert(Correction Notice): Writing a new page in the history book saying βThe entry on page 10 was incorrect; page 12 cancels out those changes.β
π Modes & Syntax Comparison
1. git reset Modes (Local Unpushed Commits Only!)
Moves HEAD pointer back by 1 commit (HEAD~1) or to a specific SHA hash:
# Soft Reset: Uncommits, but keeps all changes STAGED in Index
git reset --soft HEAD~1
# Mixed Reset (Default): Uncommits, keeps changes UNSTAGED in Working Directory
git reset --mixed HEAD~1
git reset HEAD~1
# Hard Reset (DANGER!): Uncommits AND wipes out all working directory edits!
git reset --hard HEAD~1
2. git revert (Safe for Shared Remote Branches!)
Creates a new inverse commit undoing specified commit:
git revert a1b2c3d
π‘ Practical Example
Comparison workflow between reset and revert:
# Scenario A: Local Soft Reset
# Undo last commit, keep changes staged to fix commit message or add missing file
git reset --soft HEAD~1
git add missing_file.js
git commit -m "Corrected commit with missing file"
# Scenario B: Safe Public Revert
# Safely undo commit a1b2c3d on shared main branch
git revert a1b2c3d -m "Revert broken authentication payment gateway"
π Command Breakdown
HEAD~1: Relative notation meaning β1 commit prior to currentHEADβ. (HEAD~2= 2 commits back).git revert <hash>: Opens text editor asking for a commit message for the new reverting commit.
π Terminal Output
# Executing git revert:
$ git revert a1b2c3d
[main e5f6g7h] Revert "Add buggy discount calculation"
1 file changed, 2 deletions(-)
# Notice that git log now shows BOTH original commit AND reverting commit!
β οΈ Critical Danger Warning
- NEVER Reset Pushed Public Commits: Executing
git reset --hardon commits already pushed to GitHub breaks history for all teammates pulling from origin. This forces chaotic merge conflicts for the entire team.- Golden Rule: Reset for Local Unpushed Code; Revert for Shared Pushed Code!
π‘οΈ Safety / Important Notes
- If you accidentally execute
git reset --hard, Git stores a safety backup log calledgit reflog. You can recover lost commits usinggit reflogwithin 30 days!
π Real-World Usage
DevOps engineers use git revert during production incidents to instantly undo breaking deployment commits in main pipelines without rewriting git history.
π§ͺ Try It Yourself
- In your
git-practicerepo, make a dummy commit:echo "Test" >> test.txt,git add .,git commit -m "Dummy commit". - Run
git log --onelineand notice your commit. - Run
git reset --soft HEAD~1and rungit status. Noticetest.txtis still staged!
π― Mini Challenge
Commit test.txt again. Now run git revert HEAD and inspect git log --oneline to see the new reverting commit generated by Git.
π Related Topics
π§ Navigation
| β Home | β Previous: Restore | Next: Branching Basics β |