git revert

Basic Snapshotting

Undo a commit by creating a new commit


The 'git revert' command undoes an earlier commit by adding a new commit that applies the opposite change. History is not rewritten — the original commit stays — so it is the safe way to undo commits that were already pushed and shared. It can conflict when later commits touched the same lines; resolve, then git revert --continue.

Synopsis

git revert [--[no-]edit] [-n] [-m <parent-number>] <commit>...
git revert (--continue | --skip | --abort | --quit)
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (1)

Do not open the editor; keep the default "Revert …" message.

RecommendedThe default revert message is usually right

Example

git revert --no-edit HEAD

Intermediate (1)

Apply the opposite changes to the working tree and staging area without committing.

When neededCombine several reverts into one commit

Example

git revert -n HEAD HEAD~1

Advanced (1)

For a merge commit: the parent number (usually 1) whose side is kept.

When neededThe only way to revert a merge commit

Example

git revert -m 1 HEAD

Choosing the Right Option

revert vs reset

Two ways to undo commits

CommandHistorySafe after push?
git revert <commit>Adds an undo commitYes
git reset --hard <commit>Removes commits from the branchNo — needs a force push

Examples

Undo the last commit

git revert HEAD

Creates a commit that undoes HEAD.


Undo an older commit

git revert a1b2c3d

Undoes that commit, leaving later ones.


Undo a merge

git revert -m 1 <merge-commit>

Undoes the changes a merge brought in.

Workflow Examples

1
git log --oneline

Find the commit

2
git revert a1b2c3d

Create the undo commit

3
git push

Share it — no force needed

Troubleshooting

error: commit is a merge but no -m option was given

Cause: A merge has two parents

Use git revert -m 1 <commit>

CONFLICT during revert

Cause: Later commits changed the same lines

Fix the files, git add them, then git revert --continue (or --abort)

Tips & Pitfalls

Best Practices

  • ✓Revert shared commits; reset only local ones.
  • ✓Explain why in the revert message.
  • ✓Revert newest first when undoing several commits.

Common Mistakes

  • ✗Using git reset on pushed commits instead of git revert, and forcing everyone to fix their history.
  • ✗Reverting a merge without -m.
  • ✗Re-merging a reverted branch and wondering why its changes do not come back — revert the revert first.

FAQ

A:

No. It adds a new commit; the original stays in history.

A:

Yes — it re-applies the original change.

A:

git revert --abort.