Reverting Commits
Create a new commit that undoes changes from a previous commit.
Revert vs Reset
| Command | Effect | Safe for Shared Branches |
|---|---|---|
revert | Creates new undo commit | ✓ Yes |
reset | Rewrites history | ✗ No |
Basic Revert
Revert Commands
| Command | Description |
|---|---|
git revert <commit> | Revert specific commit |
git revert HEAD | Revert last commit |
git revert HEAD~3 | Revert commit 3 ago |
git revert --no-commit | Revert without committing |
Reverting a Single Commit
git revert abc1234
This creates a new commit that undoes the changes from abc1234.
Reverting the Last Commit
git revert HEAD
Reverting Multiple Commits
# Revert range (oldest to newest)
git revert abc1234..def5678
# Revert multiple specific commits
git revert abc1234 def5678 ghi9012
Revert Options
Without Auto-Commit
git revert --no-commit HEAD
Stages the revert changes but doesn't commit. Useful for:
- Combining multiple reverts
- Making additional changes
Continue/Abort
If revert has conflicts:
git revert --continue # After resolving
git revert --abort # Cancel revert
Handling Conflicts
When reverting causes conflicts:
- Resolve conflicts in files
- Stage resolved files:
git add . - Continue:
git revert --continue
When to Use Revert
- Undoing changes on shared branches
- Removing a feature after release
- Fixing bugs introduced by a specific commit
Exercise: Revert a Commit
Revert the most recent commit:

