# Safely Undoing Commits

> How to use `git revert` to safely undo shared commits without rewriting history.

Subject: Version Control | Lesson 19 of 29 | 5 min read
Interactive lesson: https://codewithanimation.in/git/git-revert
Previous: [Discarding Uncommitted Changes](https://codewithanimation.in/git/git-restore.md) | Next: [Undoing Commits: Reset](https://codewithanimation.in/git/git-reset.md)

## The git revert command

`git revert` is a safe, non-destructive undo operation for commits.

Instead of removing a commit from the project history, it figures out how to invert the changes introduced by a specified commit, and appends a brand-new commit with that resulting inverse content.

## Why it matters

It is the absolute safest way to undo a bug that has already been pushed to a public remote branch.

Because it doesn't rewrite or erase existing history, it prevents catastrophic merge conflicts for other developers who are collaborating on the same codebase.

## How it works

It takes a specified target commit (e.g., `git revert a1b2c3d`), calculates the exact opposite of its file changes (lines added become lines removed).

Then, it creates a brand-new 'revert commit' with these opposing changes. The `HEAD` pointer moves forward to this new commit, effectively undoing the bug while leaving a transparent audit trail.

## Common mistake: The 'Delete' Misconception

Many beginners confuse `revert` with `reset` and expect the buggy commit to completely disappear from the git log.

In reality, the bad commit stays exactly where it is in the history, and a new 'anti-commit' is added on top of it to cancel it out.

## Tip: Moving history forward to go backward

Mental Model: Think of `git revert` as adding a negative version of a previous commit, rather than using an eraser.

For example, `git revert HEAD` creates a new commit that perfectly opposes the changes introduced in your very last commit.

## Try it: Undo with a new commit

Revert a sample change while preserving the original commit in the history.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-revert

## Check yourself

### Question 1

What does `git revert` do to your project's commit history?

- A. It deletes the specified commit and all subsequent commits.
- B. It deletes only the specified commit from the middle of the history.
- C. It creates a new commit that applies the inverse of the changes from a specified commit.
- D. It moves the HEAD pointer backward to the specified commit.

Correct answer: C

`git revert` is a safe undo operation that preserves history by adding a new commit that directly cancels out the changes of the target commit.

- Why not A: This describes `git reset --hard`.
- Why not B: Git does not allow deleting a commit from the middle without rewriting.
- Why not D: This is what `git checkout` or `git reset` does.

### Question 2

Why is `git revert` preferred over `git reset` when undoing a bug on a public/shared branch?

- A. Because `git revert` automatically writes code to fix the bug.
- B. Because `git revert` preserves history and doesn't rewrite public commits, preventing conflicts.
- C. Because `git reset` cannot be used on commits older than 24 hours.
- D. Because `git revert` executes faster on remote servers.

Correct answer: B

Rewriting public history with `git reset` causes severe issues for anyone who has already pulled those commits. `git revert` safely adds a new commit, keeping everyone's history perfectly aligned.

- Why not A: It just inversely applies changes.
- Why not C: Git has no time limits.
- Why not D: Performance is not the reason.

### Question 3

If you run `git revert HEAD`, what happens?

- A. A new commit is created that undoes the changes made in the very last commit.
- B. The current uncommitted changes in your working directory are discarded.
- C. The last commit is permanently erased from the branch history.
- D. The entire repository is reset to its initial state.

Correct answer: A

`HEAD` points to your latest commit. `git revert HEAD` creates a new commit that perfectly opposes the changes introduced in that exact last commit.

- Why not B: To discard uncommitted changes, you use `git restore`.
- Why not C: `git revert` never erases commits.
- Why not D: `HEAD` refers to the latest commit, not the initial one.

## Remember this

- `git revert` safely undoes a commit by creating a new 'anti-commit'.
- It never deletes or rewrites history, making it perfectly safe for shared branches.
- Think of it as moving history forward to go backward.
