← Back to Chapters

Git Revert

? Git Revert

? Quick Overview

Git revert is used to undo a committed change by creating a new commit that reverses a previous commit. It keeps the repository history intact and is safe for public branches.

? Key Concepts

  • Creates a new commit that undoes another commit
  • Does not rewrite commit history
  • Safe for collaboration

? Syntax & Theory

The basic syntax of git revert requires a commit hash to identify which commit should be undone.

? View Code Example
// Revert a specific commit using its hash
git revert <commit-hash>

? Code Examples

? View Code Example
// Revert the most recent commit
git revert HEAD
? View Code Example
// Revert multiple commits using a range
git revert <commit-hash>^..<commit-hash>

? Live Output / Explanation

A new commit is created that reverses the changes. The commit history remains intact and collaboration is unaffected.

? Interactive Explanation

Think of git revert as applying an opposite patch. Instead of removing history, it appends a corrective step.

? Use Cases

  • Undo a faulty commit in production
  • Safely rollback changes on shared branches
  • Maintain a clean and auditable history

? Tips & Best Practices

  • Prefer git revert over git reset on shared branches
  • Revert small, focused commits for clarity
  • Test after reverting to ensure stability

? Try It Yourself

  • Revert the latest commit using HEAD
  • Revert a commit by hash
  • Revert a merge commit using -m