Reverting Commits
24.1 What Reverting a Commit Means
Reverting creates a new commit that undoes the changes introduced by a previous commit. The original commit remains in history, making revert safe for shared branches.
24.2 Basic Revert Command
To revert a single commit:
Example:
Git opens an editor to confirm the revert commit message.
1git revert <commit-hash>Example:
1git revert a1b2c3dGit opens an editor to confirm the revert commit message.
24.3 Reverting the Latest Commit
To undo the most recent commit:
This is the safest way to undo a bad commit on main or develop.
1git revert HEADThis is the safest way to undo a bad commit on main or develop.
24.4 Reverting Multiple Commits
To revert a range of commits:
Example:
Each commit is reverted individually.
1git revert <oldest>..<latest>Example:
1git revert HEAD~3..HEADEach commit is reverted individually.
24.5 Revert Without Auto-Commit
To review changes before committing:
This applies the inverse changes but leaves them staged.
1git revert --no-commit <commit-hash>This applies the inverse changes but leaves them staged.
24.6 Handling Revert Conflicts
Conflicts may occur if later commits depend on reverted changes.
Steps:
1. Resolve conflicts manually
2. Stage resolved files
3. Continue revert:
Stashing is safer and reversible (covered later).
Steps:
1. Resolve conflicts manually
2. Stage resolved files
3. Continue revert:
1git revert --continueStashing is safer and reversible (covered later).
24.7 When to Use Revert vs Reset
• Use revert on shared branches
• Use reset only for local history cleanup
• Revert preserves collaboration safety
• Use reset only for local history cleanup
• Revert preserves collaboration safety
24.8 Key Takeaways
• git revert undoes changes by creating a new commit.
• Reverting does not rewrite history.
• Safe for public and shared branches.
• Can revert single or multiple commits.
• Prefer revert over reset on pushed commits.
• Reverting does not rewrite history.
• Safe for public and shared branches.
• Can revert single or multiple commits.
• Prefer revert over reset on pushed commits.