How do I "un-revert" a reverted Git commit?

Learn how do i "un-revert" a reverted git commit? with practical examples, diagrams, and best practices. Covers git, undo, revert development techniques with visual explanations.

How to 'Un-Revert' a Reverted Git Commit

Hero image for How do I "un-revert" a reverted Git commit?

Learn the strategies and commands to effectively undo a git revert operation, restoring your codebase to a previous state.

Git's revert command is a powerful tool for undoing changes without rewriting history. It creates a new commit that negates the effects of a previous commit. However, what happens if you revert a commit, and then realize you actually needed those changes back? This article will guide you through the process of 'un-reverting' a reverted commit, explaining the different scenarios and the appropriate Git commands to use.

Understanding git revert

Before we dive into un-reverting, it's crucial to understand how git revert works. Unlike git reset, which moves the branch pointer and potentially discards history, git revert creates a new commit. This new commit applies the inverse changes of the specified commit, effectively canceling its effects while preserving the project's history. This makes revert a safe operation for shared branches.

flowchart LR
    A[Original Commit] --> B[Feature Commit]
    B --> C["Revert Feature Commit (New Commit)"]
    C --> D["Un-Revert (Revert the Revert)"]

Conceptual flow of a commit, its revert, and subsequent un-revert

Method 1: Reverting the Revert Commit

The most straightforward way to 'un-revert' a commit is to simply revert the revert commit itself. Since a revert commit is just another commit in your history, you can apply the git revert command to it. This will create a third commit that re-introduces the changes that were originally undone by the first revert. This method is generally recommended because it maintains a clean, linear history and is safe for shared repositories.

# 1. Identify the SHA of the revert commit
#    Use `git log` to find the commit that reverted your original changes.
#    Look for a commit message like "Revert 'Your original commit message'"

git log --oneline

# Example output:
# a1b2c3d Revert 'Add new feature'
# e4f5g6h Add new feature
# ...

# 2. Revert the revert commit
git revert <SHA_of_revert_commit>

Reverting a revert commit using its SHA

Method 2: Using git reset (Use with Caution!)

While git revert is preferred for its safety, in some very specific scenarios (e.g., you just made the revert locally and haven't pushed it yet, or you are absolutely sure no one else has pulled your changes), you could use git reset. However, git reset rewrites history, which can cause significant problems for collaborators if the history has already been shared. This method should be used with extreme caution and only when you fully understand its implications.

# Scenario: You just reverted a commit and haven't pushed yet.
# You want to completely remove the revert commit from your local history.

# 1. Identify the commit *before* the revert commit.
#    `HEAD~1` refers to the commit immediately before the current HEAD.
#    If the revert commit is the latest, `HEAD~1` will be the commit before it.

git log --oneline

# Example output:
# a1b2c3d Revert 'Add new feature' (HEAD)
# e4f5g6h Add new feature
# ...

# 2. Reset to the commit *before* the revert commit.
#    `--hard` will discard all changes in your working directory and staging area.
#    `--soft` will keep changes in the staging area.
#    `--mixed` (default) will keep changes in the working directory but unstage them.

git reset --hard HEAD~1

Using git reset --hard to remove a local revert commit

Choosing the Right Approach

The decision between reverting the revert and using git reset largely depends on whether the revert commit has been shared with others. If the revert commit is already on a remote branch that others have pulled, always opt for reverting the revert. This preserves history and avoids disrupting collaborators. If the revert is purely local and you're certain no one else has seen it, git reset can be used to clean up your local history, but it's generally safer to stick with git revert even in this case, as it's a more consistent and less error-prone workflow.