- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git revert Tutorial
Explains why git revert is the safe way to undo shared commits, and how it differs from reset at the history level.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
Data & Performance
- new commitunlike reset, revert adds history rather than removing it — safe on public branchesSource: git-revert(1) official manual
Key Quotes
Reverting undoes a commit by creating a new commit that applies the inverse of the original change — safe for shared history because it does not rewrite it.
Citations & Further Reading
- Git revert [Official]
- Git Basics Undoing Things [Book]
What you will learn
- Understand the core purpose of git revert Tutorial
- Master the basic usage and common options of git revert Tutorial
- Explains why git revert is the safe way to undo shared commits, and how it differs from reset at the history level.
- Understand key concepts: Why this matters in teams
- Know when to use this feature and when to avoid it
Start with a problem
You're working in a Git repository and need to perform a specific task — but you're not sure which command or option is the right fit, or what this command can and cannot do.
The short version
git revert does not remove an old commit. Instead, it creates a new commit that reverses the effect of an earlier one.
Why this matters in teams
If a bad commit has already been pushed to a shared branch, rewriting public history with reset can disrupt collaborators. Revert is safer because it preserves the history and records the undo explicitly.
Basic usage
git revert <commit>
Git creates a new commit whose diff cancels the target commit.
Revert several commits
git revert older_commit^..newer_commit
This is useful when a whole sequence needs to be backed out while still preserving the historical record.
Stage the reverse changes without committing yet
git revert --no-commit <commit>
That is useful when you want to combine several reverts into one reviewed commit.
Reverting a merge commit
The official documentation calls out the need to choose a mainline parent:
git revert -m 1 <merge-commit>
Here, -m selects the parent Git should treat as the main line of history.
revert vs reset
revertkeeps history and adds an undo commitresetmoves refs and may rewrite history
As a practical rule, prefer revert when the commit is already shared.
What problem this command solves in a workflow
git revert directly affects history shape, ref position, or relationships between commits. The first decision is whether you are organizing private local history or touching history that is already shared with others.
Typical use cases
- Use
git revertwhen integrating branches, undoing changes, selecting commits, or reshaping a sequence of commits. - Put
git revertinside a “backup first, mutate second, verify last” flow so higher-risk history operations stay recoverable. - Use
git revertto understand ancestry, recovery paths, and commit causality during conflict resolution or post-incident analysis.
Diagram view
Special cases and boundaries
- The most expensive failure mode in history commands is rewriting or moving commits that other people already depend on.
- If the operation might affect team flow, run
git status,git log --oneline --graph, andgit reflogfirst so the recovery path is visible. - When in doubt, create a backup branch before continuing so you preserve an obvious way back.
- Reverting a merge commit usually requires
-mso Git knows which parent should be treated as the mainline.
Try it yourself
- Practice the git-revert command in a test repository and observe state changes before and after
- Experiment with different options and compare the output differences
- Simulate a real scenario where you would need to use this, and walk through the full process
Further reading
Keep going on the same topic: