Git Revert

1 min

Table of contents

  1. What Is Revert?
  2. Revert vs Reset
  3. Immediate Practice
  4. Use Cases

1. What Is Revert?

Git revert = Undo ONE commit by creating a NEW inverse commit.

Key concept:

  • Does NOT delete the history
  • Creates a commit that "undoes" the changes
  • Safe for teamwork

Analogy: It is like saying "Oops, I take back what I said" publicly.

Back to table of contents

2. Revert vs Reset

Aspectgit revertgit reset
HistoryKeptModified/Deleted
SafetySafe in a teamDangerous if shared
MethodNew inverse commitGoes back in time
TraceabilityVisible that it was undoneAs if it had never existed

Simple rule:

  • Already shared commit → revert
  • Still local commit → reset

Back to table of contents

3. Immediate Practice

bash
mkdir demo-revert && cd demo-revert
git init

echo "print('Version 1')" > app.py
git add . && git commit -m "Version 1"

echo "print('Version 2 - Bug!')" > app.py
git add . && git commit -m "Version 2 with bug"

echo "print('Version 3')" > app.py
git add . && git commit -m "Version 3"

# Oops! Version 2 had a bug
git log --oneline              # See the history
git revert <hash-version-2>    # Undo JUST version 2

# Result: Version 3 stays, but the v2 bug is undone!
cat app.py                     # Check the content

Back to table of contents

4. Use Cases

Typical situations:

  1. Bug in production:

    bash
    git revert <commit-buggy>    # Clean undo
    git push origin main         # Immediate fix
  2. Feature causes problems:

    bash
    git revert <commit-feature>  # Remove the feature
    # Keep the history for later
  3. Undo several commits:

    bash
    git revert <commit1> <commit2> <commit3>
    # Or in one go:
    git revert <oldest-commit>..<newest-commit>

Revert = Your safety net when everything goes wrong!

Back to table of contents