Git Revert
The git revert command creates a new commit that undoes the changes from a specific commit. It does not delete or modify any existing commits — instead, it adds a new "reverse" commit to the history.
This makes git revert the safe way to undo changes on shared or public branches, as it preserves the full commit history and does not rewrite it.
Real-life analogy: Think of a ledger book. An accountant made an incorrect entry on line 50. Instead of erasing it (which would be dishonest), they add a new entry on line 60 that corrects the mistake. The original error is still visible, but the new entry cancels it out. This is how git revert works.
git revert vs git reset
| Feature | git revert | git reset |
|---|---|---|
| Rewrites history | No — adds a new commit | Yes — removes commits |
| Safe for shared branches | Yes | No |
| Original commit remains | Yes — visible in history | No — it disappears |
| Best used for | Undoing pushed commits | Undoing local/unpushed commits |
Basic Syntax
git revert <commit-hash>Practical Example
# View the commit history
git log --oneline
# a3f92b1 (HEAD) Add login page
# b7c3d45 Update homepage title to "Welcome!"
# c1a2b3c Initial commit
# Oh no! The homepage title change in b7c3d45 was a mistake.
# Revert that specific commit:
git revert b7c3d45
Git opens a text editor with a default message like:
Revert "Update homepage title to Welcome!"
This reverts commit b7c3d45.
Save and close the editor to complete the revert.
# New history:
git log --oneline
# d2e3f4g (HEAD) Revert "Update homepage title to Welcome!"
# a3f92b1 Add login page
# b7c3d45 Update homepage title to "Welcome!" ← still visible!
# c1a2b3c Initial commit
The original commit b7c3d45 is still in the history, but the new commit d2e3f4g reverses its effects.
Revert Without Opening the Editor
git revert b7c3d45 --no-editUses the default revert commit message without opening the editor.
Revert Multiple Commits
A range of commits can be reverted:
# Revert commits from b7c3d45 up to (but not including) a3f92b1
git revert b7c3d45..a3f92b1
Revert Without Immediately Committing
If reviewing or modifying the revert before committing is needed, the --no-commit flag can be used:
git revert b7c3d45 --no-commitThis applies the reverse changes to the working directory and stages them, but does NOT create the commit yet. Modifications can be made, and then the commit can be made manually:
git commit -m "Revert homepage title change"Reverting a Merge Commit
Reverting a merge commit requires specifying the "mainline parent" — which branch's perspective to use as the base. This is done with the -m 1 flag:
git revert -m 1 <merge-commit-hash>-m 1 means "treat the first parent (main branch) as the mainline." This is almost always the correct choice when reverting a merge.
Handling Conflicts During Revert
If the code has changed significantly since the commit being reverted, a conflict may occur:
CONFLICT (content): Merge conflict in index.html
error: could not revert b7c3d45... Update homepage
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue"
Resolve the conflict in the file, then:
git add index.html
git revert --continue
To abort the revert:
git revert --abortViewing What a Revert Will Do Before Applying
git show b7c3d45This shows what the commit changed. The revert will apply the exact opposite of these changes.
When to Use git revert
- When a bad commit has already been pushed to
mainor a shared branch - When an audit trail is needed — showing both the original change and the correction
- In production environments where rewriting history is not allowed
- When working in a team and others may have already based new work on the commit being undone
Summary
git revert safely undoes a commit by creating a new "undo" commit. The original commit remains in the history — it is never deleted. This makes it the right tool for undoing changes on shared branches. Use git revert <hash> to target a specific commit and --no-edit to skip the editor prompt.
