bashbeginner

Git Revert Commit Safely

Create a new commit that undoes changes from a previous commit without rewriting history.

bash
# Revert the most recent commit
git revert HEAD

# Revert a specific commit by hash
git revert abc1234

# Revert without auto-committing (stage changes only)
git revert --no-commit abc1234

# Revert multiple commits
git revert abc1234 def5678

# Revert a range of commits
git revert abc1234..ghi9012

# Revert a merge commit (specify parent)
# Parent 1 is main, parent 2 is the merged branch
git revert -m 1 merge-commit-hash

# If conflicts during revert:
git add .
git revert --continue

# Abort the revert
git revert --abort

# Verify the revert
git log --oneline -5
git diff HEAD~1

Use Cases

  • Undoing a deployed commit on shared branches
  • Rolling back a feature without rewriting history
  • Safely reverting merge commits in production

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.