Git Revert: Safely Undo a Published Commit
Use git revert to create a new commit that undoes the changes from a previous commit. The safe way to undo work on shared branches.
git revert <commit-hash>Detailed Explanation
What Does git revert Do?
git revert creates a new commit that is the exact inverse of the specified commit. Unlike git reset, which rewrites history, revert adds to history. This makes it safe to use on shared branches where other developers have already pulled the changes.
Basic Usage
# Revert a single commit
git revert a1b2c3d
# Revert without auto-committing (stage changes only)
git revert --no-commit a1b2c3d
Reverting Multiple Commits
# Revert a range (oldest to newest)
git revert a1b2c3d..f4e5d6a
# Revert multiple individual commits
git revert --no-commit a1b2c3d
git revert --no-commit f4e5d6a
git commit -m "Revert payment and user changes"
Reverting a Merge Commit
Merge commits have two parents. You must specify which parent to revert to using -m:
# -m 1 means keep the first parent (usually the branch you merged into)
git revert -m 1 <merge-commit-hash>
Revert vs. Reset
git revert |
git reset |
|
|---|---|---|
| Modifies history | No (adds a new commit) | Yes (removes commits) |
| Safe for shared branches | Yes | No |
| Creates new commit | Yes | No |
When to Use Revert
- Undoing a commit on
mainor any shared branch — revert is the only safe option. - Rolling back a deployment — revert and push to trigger a clean rollback.
- Keeping an audit trail — the revert commit documents that a change was intentionally undone.
Use git reset only on local, unpushed commits. For everything else, git revert is the responsible choice.
Use Case
A commit deployed to production introduced a critical bug. The team uses git revert to undo the change and push the fix without rewriting shared history.