recovery

git revert

Creates a new commit that inverts the changes of a previous commit, preserving history. The safe way to undo pushed commits on shared branches.

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

Common flags

Flag Purpose
-n / --no-commit Stage the revert without creating a commit
--continue Resume after resolving conflicts
--abort Cancel an in-progress revert
-m <parent> Required when reverting a merge commit; picks the mainline
-e / --edit Edit the revert commit message

Examples

git revert HEAD

Undo the latest commit with a new inverse commit

git revert abc123..def456

Revert a range of commits, newest first

git revert -m 1 <merge-sha>

Revert a merge commit relative to its first parent

Gotcha

Reverting a merge with -m 1 makes it hard to re-merge the same branch later; you may need to revert the revert before merging again.

Related commands

← All Git commands · Git cheat sheet · Git rebase vs merge