C.W.K.
Stream
Lesson 03 of 05 · published

revert: Safe Undo for Shared History

~18 min · revert, shared-history

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

revert is the safe undo for shared history

Reset rewrites history. Revert extends history with an inverse commit. git revert <hash> creates a new commit whose changes are exactly the opposite of the target commit — same files modified, in reverse direction. The history record stays intact: the original commit is still there, and a new revert commit on top says "we undid that one." This is the only safe undo for commits that have been pushed and that others have based work on.

The semantics are clean. git revert HEAD creates a revert of the most recent commit. git revert abc1234 reverts a specific older commit, even if dozens of commits sit between it and HEAD. If the revert touches lines that have moved or been further modified, you get conflicts — same resolution loop as a merge: edit, git add, git revert --continue. git revert --no-edit skips the editor and uses Git's default message.

For a range of commits, git revert OLDEST..NEWEST creates one revert commit per source commit, in reverse order. Useful when you need to undo a sequence of related changes. --no-commit stages the revert without committing, letting you bundle multiple reverts into a single commit if that reads better in history.

The special case: reverting a merge commit. Merge commits have two parents, so Git needs to know which side is the "mainline" you want to keep. git revert -m 1 <merge-hash> reverts the merge by undoing the changes that came in from parent 2 (the merged-in branch), keeping the parent-1 line of history intact. This is the right move when a merged feature turns out to be broken and you cannot afford a full reset.

Code

Revert basics·bash
# Revert the latest commit (creates a new commit on top):
git revert HEAD

# Revert a specific older commit by hash:
git revert abc1234

# Revert without opening an editor:
git revert --no-edit abc1234

# Revert a range — one revert commit per source commit:
git revert OLDEST_HASH..NEWEST_HASH

# Stage all reverts as one combined commit:
git revert --no-commit OLDEST..NEWEST
git commit -m "Revert feature X (multi-commit rollback)"
Reverting a merge commit·bash
# After a merge that broke production:
git log --oneline --merges -n 5      # find the bad merge

# Revert the merge, keeping parent-1 (mainline) as the kept side:
git revert -m 1 <merge-hash>

# -m 1 means "the first parent is the mainline."
# If you cherry-picked from a long-running branch, you may want -m 2 instead.

# Push the revert (no force-push needed — it's a normal commit):
git push

External links

Exercise

On a branch with at least three commits, revert the middle one with git revert <hash>. Inspect git log and confirm both the original and the revert appear. Then merge another branch into the current one (creating a merge commit) and revert that merge with git revert -m 1 <merge-hash>. Read the resulting log graph and write one sentence on what the audit trail tells a reader.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.