bashadvanced
Git Revert — Undo Merge Commits Safely
Revert merge commits safely without rewriting history, with options for parent selection.
bashPress ⌘/Ctrl + Shift + C to copy
# 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 --abortUse 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.
bashbeginner
Git Revert Commit Safely
Create a new commit that undoes changes from a previous commit without rewriting history.
Best for: Undoing a deployed commit on shared branches
#git#revert
bashbeginner
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
Best for: Fixing a commit with wrong files or message
#git#undo
bashintermediate
Resolve Git Merge Conflicts
Step-by-step guide to identify, resolve, and complete merge conflict resolution in Git.
Best for: Resolving conflicts during branch merges
#git#merge
bashintermediate
Git Reflog Recovery Guide
Use git reflog to recover lost commits, undo hard resets, and restore deleted branches.
Best for: Recovering from accidental git reset --hard
#git#reflog