git revert Command Guide
Revert some existing commits
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git revert <commit>Revert a commitgit revert HEADRevert last commitgit revert HEAD~3..HEADRevert rangeOptions
-n, --no-commitDon't auto-commit-m 1Revert merge (parent 1)--no-editUse default messageControl
--continueContinue after conflict--abortAbort revertDownloadable Image Preview
Basic Usage
The git revert command creates a new commit that undoes the changes from a previous commit. Unlike reset, it preserves history and is safe for shared branches.
# Revert the last commit
git revert HEAD
# Revert a specific commit
git revert abc1234
# Revert without auto-commit
git revert --no-commit abc1234Revert vs Reset
git revert
- • Creates new commit
- • Preserves history
- • Safe for shared branches
- • Can revert any commit
git reset
- • Removes commits
- • Rewrites history
- • Only for local branches
- • Must be consecutive
git revert for commits that have been pushed to shared branches. It's the safe way to undo changes without affecting your team.Reverting Merge Commits
Merge commits have two parents. You must specify which parent to keep:
# Revert a merge commit (keep first parent - usually main)
git revert -m 1 <merge-commit>
# -m 1 means keep parent 1 (the branch you merged INTO)
# -m 2 would keep parent 2 (the branch that was merged)
# Example: Undo a feature merge into main
git checkout main
git revert -m 1 abc1234 # abc1234 is the merge commitReverting Multiple Commits
# Revert multiple commits (creates multiple revert commits)
git revert abc1234 def5678 ghi9012
# Revert a range of commits
git revert HEAD~3..HEAD
# Revert range without individual commits
git revert --no-commit HEAD~3..HEAD
git commit -m "Revert last 3 commits"
# Revert in reverse order (oldest first)
git revert --no-commit HEAD~3..HEAD
# Resolves conflicts more easilyPractical Examples
# Undo the last commit (safe for pushed code)
git revert HEAD
git push
# Undo a specific buggy commit
git revert abc1234
# Undo a merge that broke production
git revert -m 1 <merge-commit>
git push
# Undo without committing (to test or modify)
git revert --no-commit abc1234
# ... review changes ...
git commit -m "Revert: detailed explanation"
# Handle conflicts during revert
git revert abc1234
# CONFLICT: ...
# Fix conflicts in files
git add .
git revert --continue
# Abort revert if needed
git revert --abortFrequently Asked Questions
What is the difference between revert and reset?
Revert creates a new commit that undoes changes, preserving history. Reset removes commits from history. Use revert for pushed/shared commits, reset for local-only work.
How do I revert a merge commit?
Use "git revert -m 1 <merge-commit>" where -m 1 specifies to keep the first parent (usually main branch). This creates a new commit undoing the merge.
Can I undo a revert?
Yes! Just revert the revert commit. Since revert creates a new commit, you can revert it like any other commit.
How do I revert multiple commits at once?
Use "git revert --no-commit HEAD~3..HEAD" to revert multiple commits without creating individual revert commits, then commit once when ready.
Summary
git revert is the safe way to undo commits on shared branches. It creates a new commit, preserving full history for your team.
Quick Reference
git revert <commit>- Revert a commitgit revert -m 1 <merge>- Revert mergegit revert --no-commit- Revert without commitgit revert --abort- Cancel revert
Official Documentation
For authoritative information, refer to the official documentation: