Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 78 ยท Rewriting History

Reverting Commits Safely

Use git revert to undo a commit without rewriting history: single and multiple commits, merge commits with -m, reverting a revert, conflicts, and revert versus reset.

A bad commit reached main, and other people have already pulled it. You cannot reset or rebase it away without rewriting shared history. The safe tool is git revert: it creates a new commit whose changes are the exact opposite of an earlier commit. The old commit stays in the history, and so does the revert, so the record of what happened is complete and nobody's clone breaks.

This lesson covers reverting one commit, several commits, a merge commit (which needs -m), what to do when a revert conflicts, why you sometimes need to "revert the revert", and how to choose between revert and reset.

Reverting one commit

git revert <commit> computes the diff that the commit introduced, applies the inverse to your current files, and commits it. Because the inverse is applied on top of today's state, it works for any commit in the past, not just the last one.

TEXT
Before:   A---B---C---D   main        (C introduced a bug)

git revert C

After:    A---B---C---D---C'  main    (C' = "Revert C": the opposite of C)
Bash Revert a commit from the middle of history

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

A fourth commit, Revert "Turn debug on", was added. Its message is generated automatically and mentions the hash it undoes: This reverts commit <hash>. The debug=on line is gone from config.txt, but other.txt (a later, unrelated commit) is untouched. Without --no-edit, Git opens your editor to let you improve the message. A good revert message says why the change was undone.

Revert without committing

-n (--no-commit) applies the inverse to your working tree and index but stops before committing. That is useful for combining reverts, or for reverting only part of a commit (stage what you want, discard the rest).

Bash Revert with -n and look at the result first

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

After revert -n both files were staged (M ). We unstaged and restored a.txt, and committed a revert that touches only b.txt. Result: a partial revert, which plain git revert cannot do.

Reverting several commits

You can pass several commits or a range. Git creates one revert commit per original commit, newest first, because that order avoids conflicts between them.

TEXT
git revert A B C            # three commits, each reverted separately
git revert HEAD~3..HEAD     # the last three commits (range: excludes HEAD~3, includes HEAD)
git revert -n HEAD~3..HEAD  # same, but stop and let me create one combined commit
Bash Revert the last three commits with one combined commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Without -n you would get three commits (Revert "Experiment C", Revert "Experiment B", Revert "Experiment A"). With -n they were combined into a single commit. The tree is now identical to Base, yet the history still shows that the experiments happened.

Reverting a merge commit

A merge commit has two parents, and git revert cannot know which side of the merge you want to keep. Without help it refuses. The -m option picks the mainline parent: parent number 1 is the branch you were on when you merged (usually main), parent 2 is the branch that was merged in. git revert -m 1 <merge> means: "undo everything the merge brought in, and keep the first parent's line."

Bash Revert a merge commit (fails on purpose, then fixed with -m 1)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first attempt failed with error: commit <hash> is a merge but no -m option was given. With -m 1, feat.txt was removed while main.txt (committed on main itself) stayed. Notice what a merge revert does not do: it does not undo the history of the merge, only its changes. Git still believes feature has been merged. This leads to a well-known trap.

The re-merge trap: revert the revert

After you revert a merge, you fix the feature branch and want to merge it again. A plain git merge feature brings in only the new commits since the earlier merge, because Git considers the old ones already merged. The original feature changes stay missing. The solution is to first revert the revert, which restores those changes, and then merge the fixed branch.

Bash Revert a revert to bring a feature back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The naive re-merge listed fix.txt but not feat.txt, which is exactly the trap: only the new commit arrived. After resetting that merge away (it was not pushed), reverting the revert restored feat.txt, and merging feature again brought everything: the final list has feat.txt, fix.txt, main.txt and base.txt. Alternative: rebuild the feature branch as new commits (for example with git rebase, or cherry-pick) so the old merge no longer matters.

Conflicts while reverting

Reverting an old commit is a three-way merge, and later commits may have changed the same lines. Then Git stops and asks you to resolve, in the same way as for merges and cherry-picks. The three ways out are:

Command Meaning
git revert --continue conflicts resolved and staged; create the revert commit
git revert --skip skip this commit (when reverting several)
git revert --abort give up and return to the state before the revert
Bash A revert that conflicts, then resolved

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The conflict arises because the revert wants to turn 50 back into 10, but the line says 100. Git cannot decide, so it writes conflict markers into settings.txt. You decide the right value (here we accept the revert and go back to 10), stage the file and continue. Had you kept 100, the resolved file would equal the current one and Git would have nothing to commit, so the revert simply ends without creating a commit.

Revert or reset?

Situation Use
The commit is already pushed and others may have it git revert
The commit is only on your machine git reset (or commit --amend)
You need an audit trail of the undo git revert
You want the history to look as if it never happened git reset / rebase, on private branches only
You want to undo a commit in the middle, not at the tip git revert (reset would also remove later commits)
Production is broken and you need a fast, safe undo git revert and deploy

Revert is the right "panic button" for a broken main. It is safe, quick, can be reviewed like any other commit, and can itself be reverted later when the fix is ready. Create the revert on a branch and open it as a pull request, as with other changes, or push it straight away for an emergency.

Pitfalls and good habits

  • A revert does not delete data from history. A revert of a secret leaves the secret in the earlier commit. See Amending, Splitting and Rewriting Commits.
  • Revert the commit that introduced the problem, not the latest one. Find it with git log -S or git bisect.
  • Write why in the message. Revert "Add caching": breaks login for SSO users helps the person who later wants to bring the feature back.
  • A revert is itself a commit, so git revert <revert-commit> re-applies the original change ("revert of a revert"), and git cherry-pick of the original also works.
  • Dirty trees: git revert refuses to start with uncommitted changes in files it must touch. Commit or stash first.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does git revert <commit> do?

    Revert adds history instead of removing it, so it is safe for commits that are already shared.
  2. 2. Why does git revert on a merge commit need -m?

    Git needs to know which side of the merge to keep. -m 1 means the first parent, usually the branch you merged into.
  3. 3. You reverted a merge of a feature branch, fixed the feature, and merged it again, but the original feature changes are missing. Why?

    Reverting a merge undoes its changes, not the merge relationship. Reverting the revert restores the changes, and then a new merge works.
  4. 4. Which command creates one combined commit when undoing the last three commits?

    -n applies the inverse changes without committing, so you can create a single commit afterwards.
  5. 5. A bad commit is on main and teammates have pulled it. Which is the appropriate undo?

    Revert adds a new commit, so teammates just pull it. A reset plus force push would rewrite shared history.

Summary

  • git revert <commit> adds a new commit that undoes an earlier one; history is never rewritten, so it is safe on shared branches.
  • -n stops before committing, which allows combining several reverts or reverting part of a commit; --no-edit accepts the default message.
  • Several commits are reverted newest first, one revert commit each.
  • Merge commits need -m 1 (or -m 2) to pick the mainline parent. After reverting a merge, revert the revert before re-merging the fixed branch.
  • Conflicts are resolved like merges: edit, git add, git revert --continue, or --skip / --abort.
  • Use revert for pushed commits and reset for private ones.

Next: Remotes: clone, remote, fetch