bashadvanced

Git Revert — Undo Merge Commits Safely

Revert merge commits safely without rewriting history, with options for parent selection.

bash
# Revert a regular commit
git revert abc1234

# Revert without auto-committing
git revert --no-commit abc1234
git revert --no-commit def5678
git commit -m "revert: undo broken auth changes"

# --- Revert a MERGE commit ---
# Merge commits have 2 parents. You must specify which parent to keep.
# Parent 1 (-m 1): the branch you merged INTO (usually main)
# Parent 2 (-m 2): the branch you merged FROM (feature branch)

# Revert a merged PR (keep main's history, undo feature changes)
git revert -m 1 abc1234

# View merge commit parents
git log --oneline abc1234 -1
git cat-file -p abc1234 | head -3
# tree ...
# parent aaa1111  ← parent 1 (main)
# parent bbb2222  ← parent 2 (feature)

# --- Re-land a reverted merge ---
# If you revert a merge and later want those changes back:
git revert <revert-commit-hash>
# This "reverts the revert", re-applying the original changes

# --- Revert a range of commits ---
git revert --no-commit HEAD~3..HEAD
git commit -m "revert: undo last 3 commits"

# Abort a revert in progress
git revert --abort

Use Cases

  • Rolling back a broken deployment without force push
  • Safely undoing merged PRs in production
  • Re-landing previously reverted features

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.