By admin , 29 April 2026

Synopsis

git revert [-n] [-m <parent>] <commit>...

Description

The git revert command creates a new commit whose changes are the inverse of an earlier commit's. Unlike git reset, it doesn't rewrite history — it adds a new commit that undoes one or more old ones. This makes revert safe to use on shared branches: collaborators see a clear "undo" commit rather than rewritten history.

To revert a merge commit, use -m 1 (or whichever parent number) to specify the "mainline" — the parent whose perspective should be preserved. Reverting multiple commits in a single command applies them as a series of new commits.

In day-to-day use, git revert integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git revert --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git revert interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git revert's side effects helps avoid all three.

Common Options

OptionDescription
-n, --no-commitStage the inverse changes without committing.
-m <parent-number>Mainline parent for reverting a merge.
--continueResume after resolving conflicts.
--abortCancel an in-progress revert.
--skipSkip the current commit and move on.
-e, --editEdit the revert commit message.

Examples

git revert HEAD
# Undo the most recent commit with a new commit

git revert abc123..def456
# Revert a range of commits

git revert -m 1 merge-sha
# Revert a merge commit, keeping mainline

git revert -n HEAD~2
# Stage an inverse without committing yet

Common Mistakes

Reverting a merge prevents future re-merging of the same branch unless you also revert the revert. Plan accordingly when reverting feature merges. Conflicts during revert require --continue or --abort, just like rebase.

Related Commands

git reset, git restore, git cherry-pick, git log