Git revert = Undo ONE commit by creating a NEW inverse commit.
Key concept:
Analogy: It is like saying "Oops, I take back what I said" publicly.
| Aspect | git revert | git reset |
|---|---|---|
| History | Kept | Modified/Deleted |
| Safety | Safe in a team | Dangerous if shared |
| Method | New inverse commit | Goes back in time |
| Traceability | Visible that it was undone | As if it had never existed |
Simple rule:
revertresetmkdir demo-revert && cd demo-revert
git init
echo "print('Version 1')" > app.py
git add . && git commit -m "Version 1"
echo "print('Version 2 - Bug!')" > app.py
git add . && git commit -m "Version 2 with bug"
echo "print('Version 3')" > app.py
git add . && git commit -m "Version 3"
# Oops! Version 2 had a bug
git log --oneline # See the history
git revert <hash-version-2> # Undo JUST version 2
# Result: Version 3 stays, but the v2 bug is undone!
cat app.py # Check the contentTypical situations:
Bug in production:
git revert <commit-buggy> # Clean undo
git push origin main # Immediate fixFeature causes problems:
git revert <commit-feature> # Remove the feature
# Keep the history for laterUndo several commits:
git revert <commit1> <commit2> <commit3>
# Or in one go:
git revert <oldest-commit>..<newest-commit>Revert = Your safety net when everything goes wrong!