Reverting a commit

Intermediate · 9 min

Undo a commit that is already published by adding a new commit that reverses it, including the extra step needed for merge commits and how to reapply one.

Before thisReset, soft, mixed and hardMerging: fast-forward and three-way

The Modern SDE · 9 min

Reverting a commit

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by The Modern SDEReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `git revert <commit>` creates a new commit that applies the opposite of that commit's changes, so history is preserved and safe to push.
  • You can revert any commit in the middle of history, not only the latest one, and Git may stop with a conflict if later changes touch the same lines.
  • `git revert -n` applies the reversal to your working tree and index without committing, so you can combine several reverts or adjust the result.
  • Reverting a merge commit needs `-m 1` to say which parent is the mainline, otherwise Git refuses.
  • After reverting a merge, merging the same branch again does nothing, so you must revert the revert (or recreate the branch) to bring its changes back.

Undoing without rewriting

You merged a commit that adds timestamps to jot, and it breaks listing. Other people have already pulled it. Reset would erase the commit from your branch, but their copies still have it, and you’d have to force push to publish the erasure.

git revert takes the other route: history only grows. It builds a new commit whose changes are the exact inverse of an old one. The bad commit stays in the log, followed by its undo, and anyone can pull the fix like any other commit.

Reverting one commit, even a buried one

My repository had four commits, and the bad one was the third of four:

$ git log --oneline
ef8bd8a Add other.txt
d7891f7 Add timestamps
b97f938 Add second note
c6ecbe5 Add notes.txt
$ git revert --no-edit HEAD~1
[main 1d07b4c] Revert "Add timestamps"
 1 file changed, 1 deletion(-)
$ git log --oneline
1d07b4c Revert "Add timestamps"
ef8bd8a Add other.txt
d7891f7 Add timestamps
...

(Hashes will differ for you.) I reverted HEAD~1, the commit in the middle, and left “Add other.txt” alone. Revert doesn’t need the bad commit to be the latest. If a later commit changed the same lines, Git stops with a conflict, and you resolve it like any merge conflict, then run git revert --continue. --no-edit accepts the default message, which is Revert "<original subject>". In real life, write a line saying why.

Staging the reversal instead of committing

-n (--no-commit) applies the inverse and stops:

$ git revert -n HEAD
$ git status -sb
## main
M  notes.txt

Use it to revert several commits into one, or to hand-edit the result first. Then git commit as usual.

Reverting a merge

A merge commit has two parents, so “the inverse of this commit” is ambiguous: undo changes relative to which side? Git refuses to guess.

$ git log --oneline --graph
*   5871cd3 Merge feature
|\
| * 06401ae Add feature file
* | 4bf51e3 Main work
|/
$ git revert --no-edit HEAD
error: commit 5871cd3d1a5b... is a merge but no -m option was given.
fatal: revert failed
$ git revert --no-edit -m 1 HEAD
[main e6a41f9] Revert "Merge feature"
 delete mode 100644 feat.txt

-m 1 means “keep the first parent’s side”, the branch you merged into, so the feature’s changes are removed and main’s own work stays. That is nearly always what you want. Parent 2 is the branch that was merged in.

The trap: merging the branch again

You fix the feature on its branch and merge it again. Nothing happens:

$ git merge feature
Already up to date.

Git tracks which commits are in your history, and the feature’s commits still are. The revert undid their effect but didn’t remove them from history. So Git sees nothing new. The way back is to undo the undo:

$ git revert --no-edit HEAD
[main 2cd583c] Reapply "Merge feature"

On Git 2.54, a revert of a revert is titled Reapply "...". Then merge any new fixes from the branch on top. Many teams instead make a fresh branch from the fixed code.

Choosing between revert and reset

Situation Use
Commit only on your machine reset or commit --amend
Commit on a shared branch revert
Undoing a whole merged feature revert -m 1
Changing lines in an old commit and keeping the history clean interactive rebase, before pushing

What the video covers

The Modern SDE’s “Undo a Git Merge: reset, revert -m 1, and Reverting the Revert” is lesson 69 of a series, about nine minutes, from late 2026. Its title lines up with this lesson’s merge section: -m 1 and reverting the revert. It is the only video I found that treats merge reverts. I judge English from the title, description and channel only, and I could not read a transcript. The channel is small and the series is new, so treat quality as unconfirmed. Philomatics’ three-minute reset-versus-revert comparison is linked for the basic case.

Where this fits

Both reset and revert can still lose work if you pick wrong. Git keeps a private safety net for exactly that: the reflog and recovering lost work.

Check yourself

  1. 01Why use `git revert` rather than `git reset` on a commit that is already on `main`?

    Revert adds a new commit and leaves existing history alone, so everyone can pull it normally. Reset rewrites history and would need a force push.

  2. 02What does `git revert -n HEAD` do differently from `git revert HEAD`?

    It stages the reversing changes without creating the commit, so you can edit them or bundle several reverts into one commit.

  3. 03Git says 'is a merge but no -m option was given'. What does `-m 1` mean?

    A merge commit has two parents, and Git has to know which side to keep. `-m 1` means the first parent, normally the branch you merged into.

  4. 04You reverted a merge, fixed the branch, and merged it again but nothing came in. Why?

    Git still considers the branch's commits merged, and the revert only undid their effect. Revert the revert commit first, or merge a new branch.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark