LinuxCommandLibrary

git-revert

Undo a previous commit by creating a new commit

TLDR

Revert the most recent commit

$ git revert [HEAD]
copy

Revert the 5th last commit
$ git revert HEAD~[4]
copy

Revert a specific commit
$ git revert [0c01a9]
copy

Revert multiple commits
$ git revert [branch_name~5..branch_name~2]
copy

Don't create new commits, just change the working tree
$ git revert [[-n|--no-commit]] [0c01a9..9a1743]
copy

Cancel a Git revert after a merge conflict
$ git revert --abort
copy

SYNOPSIS

git revert [options] <commit>... [--] [<path>...]

PARAMETERS

-n, --no-commit
    Apply changes but do not commit; stage only

--no-edit
    Use default revert message without editor

-e, --edit
    Always open editor for revert message (default)

-m <parent-number>, --mainline <parent-number>
    For merge commits, indicate parent to revert relative to

-s, --signoff
    Add Signed-off-by trailer

-S[<keyid>], --gpg-sign[=<keyid>]
    GPG-sign commit with default or given key

--gpg-sign[=<keyid>]
    Alias for -S

-x
    Add 'Revert ""' to message

-X <strategy-option>
    Merge strategy option for revert

-R
    Alias for re-revert (inverse of revert)

--abort
    Abort revert and reset to pre-revert state

--quit
    Abort but keep index/worktree changes

--continue
    Resume after resolving conflicts

DESCRIPTION

git revert creates a new commit that reverses the changes of one or more specified commits, preserving the project's commit history. Unlike git reset, it does not rewrite history, making it ideal for shared branches where commits have been pushed publicly.

This command applies the inverse patch of the target commit(s), staging the changes for a new commit. By default, it opens an editor for the commit message, which includes details of the reverted commit. Multiple commits can be specified and are reverted in reverse chronological order.

Use cases include correcting errors in production code, backing out faulty merges, or temporarily disabling features without disrupting history. For merge commits, specify the parent branch with -m 1 (mainline) or -m 2 (merged branch). If conflicts arise, Git pauses, allowing manual resolution before continuing with git revert --continue.

It's safer for collaboration but increases history size. Always pull latest changes first to avoid conflicts.

CAVEATS

Requires -m for merge commits; conflicts need manual resolution. Does not remove commits, only counters them. Avoid on private branches (use git reset instead).

EXAMPLES

git revert HEAD - Revert last commit.
git revert abc123..def456 - Revert range.
git revert -m 1 mergeabc - Revert merge to mainline.

EXIT CODES

0: success; 1: failure (conflicts or errors); 128: invalid args.

HISTORY

Introduced in Git 1.0 (2005) by Linus Torvalds and contributors. Enhanced with merge support in 1.7.2 (2010); conflict handling improved in later versions by Junio C Hamano.

SEE ALSO

Copied to clipboard