Lesson 18 of 60 – Git Revert
30%

Git Revert

The git revert command is used to undo the changes introduced by an earlier commit. Unlike git reset, revert creates a new commit that reverses the effect of the selected commit.

Note: Git revert is especially useful when you want to undo a committed change while preserving the existing project history.

1. What is git revert?

git revert creates a new commit that reverses the changes introduced by an earlier commit.

git revert <commit>

The original commit remains in the repository history.

2. Why Use git revert?

Sometimes a commit has already been shared with other developers, but its changes need to be undone.

Instead of removing the existing commit from history, Git can create a new commit that reverses it.

  • Undo a previous commit
  • Preserve existing history
  • Create a new reversing commit
  • Safely undo changes in shared history

3. Basic Syntax

The basic syntax is:

git revert <commit>

Replace <commit> with the commit identifier that you want to reverse.

4. Finding a Commit to Revert

Use git log to find the commit you want to reverse.

git log --oneline

Example:

8f32abc Add contact page
5d21ef4 Add login page
1a72c90 Initial commit

5. Reverting a Commit

Suppose you want to reverse commit 8f32abc:

git revert 8f32abc

Git prepares a new commit that reverses the changes introduced by that commit.

6. Revert Creates a New Commit

One of the most important features of git revert is that it does not remove the original commit.

Commit A → Commit B → Commit C
                         ↓
                    Revert Commit

The new revert commit becomes part of the project history.

7. Revert vs Reset

Command Main Purpose History
git revert Undo a commit by creating a new commit Preserved
git reset Move branch reference and optionally change index/worktree Can rewrite local history

The choice depends on whether you want to preserve the existing commit history.

8. Revert a Recent Commit

To revert the latest commit, you can use:

git revert HEAD

Git creates a new commit that reverses the changes introduced by the current HEAD commit.

9. Revert HEAD~1

You can also revert the parent of the current commit:

git revert HEAD~1

Here, HEAD~1 refers to the parent of the current HEAD.

10. Revert Does Not Delete the Original Commit

Suppose the history is:

A → B → C

If you revert B, Git does not remove B. Instead, it creates a new commit that reverses B's changes:

A → B → C → Revert B

11. Revert Commit Message

When Git creates a revert commit, it normally prepares a commit message describing the reversal.

You can edit the message before completing the commit.

git revert 8f32abc

12. Revert Without Opening the Editor

The --no-edit option uses the automatically generated commit message without opening the editor.

git revert --no-edit 8f32abc

13. Reverting Multiple Commits

You can revert more than one commit by specifying a range when appropriate.

git revert HEAD~3..HEAD

This requests reversion of the commits reachable in the specified range. Review the resulting changes carefully before completing the operation.

14. Revert and Working Directory

A revert applies the inverse of a commit's changes to the current working tree and index and then creates a new commit when the operation completes successfully.

git revert 8f32abc

15. Revert Can Cause Conflicts

A revert can produce conflicts if the changes introduced by the selected commit no longer apply cleanly to the current files.

Git will stop and ask you to resolve the conflicts before completing the revert.

16. Checking for Revert Conflicts

If a conflict occurs, use:

git status

Git will show files that require attention.

17. Resolving a Revert Conflict

After opening the conflicted files, manually resolve the conflict. Then stage the resolved files:

git add filename

After all conflicts are resolved, continue the revert:

git revert --continue

18. Aborting a Revert

If you decide that you do not want to continue a revert currently in progress, you can abort it:

git revert --abort

This attempts to return the repository to the state it had before the revert operation started.

19. Revert and Shared Repositories

Revert is useful when working with shared repositories because it adds a new commit instead of removing an existing commit from history.

git revert 8f32abc
git push

The new revert commit can then be shared with other developers.

20. Revert and GitHub

If a commit has already been pushed to GitHub and needs to be undone, a revert can create a new commit that reverses the change.

git revert 8f32abc
git push

This keeps the original commit in the repository history.

21. Reverting a Bug Fix

Suppose a commit introduced a problem:

8f32abc Add new payment feature

You can reverse that commit:

git revert 8f32abc

The project can then contain a new commit that reverses the payment feature changes.

22. Revert After Checking History

git log --oneline

git show 8f32abc

git revert 8f32abc

git log --oneline

This workflow lets you inspect a commit before deciding to reverse it.

23. Revert Does Not Mean Delete

A common misunderstanding is that revert deletes a commit. It does not.

Original Commit
      ↓
Revert Commit

Both commits remain part of the history.

24. Revert and Commit History

After a successful revert, use:

git log --oneline

You can see both the original commit and the newly created revert commit.

25. Revert vs Restore

These commands have different purposes:

  • git restore restores files or staging-area content.
  • git revert creates a new commit that reverses an earlier commit.
git restore index.html

git revert 8f32abc

26. Common git revert Mistakes

  • Confusing revert with reset.
  • Assuming revert deletes the original commit.
  • Forgetting to check which commit should be reversed.
  • Ignoring conflicts during a revert.
  • Forgetting to continue after resolving conflicts.
  • Using the wrong commit identifier.
  • Not checking the repository after the revert.

27. Useful git revert Commands

git log --oneline
git show 8f32abc
git revert 8f32abc
git revert HEAD
git revert --no-edit 8f32abc
git status
git revert --continue
git revert --abort

28. Complete Revert Workflow

# View history
git log --oneline

# Inspect the commit
git show 8f32abc

# Revert the commit
git revert 8f32abc

# Check the result
git status

# View updated history
git log --oneline

This is a simple workflow for safely reviewing and reversing a commit.

29. Practical Example

git init

touch index.html
git add index.html
git commit -m "Create homepage"

touch about.html
git add about.html
git commit -m "Create about page"

git log --oneline

git revert HEAD

git log --oneline
git status

The last command sequence creates a new commit that reverses the latest commit while preserving the original commit in history.

30. Summary of git revert

The git revert command is used to undo the effect of an earlier commit by creating a new commit.

git revert <commit>

For the latest commit:

git revert HEAD

Unlike reset, revert keeps the original commit in history and adds a new commit that reverses its changes.

This makes revert particularly useful when changes have already been shared with other developers.

📌 Key Points

  • git revert reverses the effect of an earlier commit.
  • Revert creates a new commit.
  • The original commit remains in the history.
  • git revert HEAD reverts the current HEAD commit.
  • git revert --no-edit uses the generated commit message.
  • A revert can produce merge conflicts.
  • Use git status to inspect conflicts.
  • Use git revert --continue after resolving conflicts.
  • Use git revert --abort to cancel a revert in progress.
  • Revert is different from reset and restore.
  • Revert is useful for reversing changes while preserving project history.

🧠 Quick Quiz

Question: What does git revert do?