How to undo a Git revert (reset to pre-revert state)

Updated: January 29, 2024 By: Guest Contributor Post a comment

Introduction

Working with Git involves a series of changes and updates to your codebase. Sometimes, after making a revert, you may realize that you need to rollback the changes introduced by the revert itself. This tutorial will guide you through the process of undoing a Git revert to reset your repository to the pre-revert state. We will start with the basics and gradually move to more advanced topics, with code examples and outputs to help you understand each step.

Before proceeding, it’s essential to understand that reverting a revert is a delicate process. You must ensure that you’re undoing the right commit and that it won’t lead to further confusion or issues within your repository. Proceed with caution and always double-check the commit SHAs (hashes).

Identify the Revert Commit

git log --oneline

The first step in undoing a revert is to identify the commit that represents the revert itself. Use the command above to list the commits in a concise, single-line format, which makes it easier to scan through and pick out the revert commit.

Basic Undoing of a Revert

If the revert you wish to undo is the most recent commit, you can simply reset your branch to the commit before the revert:

git reset --hard HEAD^

Output:

HEAD is now at 3f6ab36 Previous Commit Message

Please note that this is a destructive action and will permanently delete any changes not committed to the repository. It moves the HEAD pointer and the current branch back to the commit just before the revert commit.

Undoing a Revert in the Middle of History

More often than not, the revert you want to undo is not the latest commit. Perhaps several other commits have been added on top of the revert. In this case, you need to take a different approach.

The safest method is to revert the revert commit. This doesn’t rewrite history; instead, it creates a new commit that applies changes inverse to those of the original revert:

git revert -m 1 REVERT_COMMIT_SHA

You need to replace REVERT_COMMIT_SHA with the SHA of the revert commit you identified earlier. The -m 1 option specifies that you’re reverting a merge commit and you want to keep the parent side of the original merge.

Reverting a Reverted Merge

Sometimes, the revert you want to undo may be reverting a merge. Merges can be more complex to handle because they involve two parent commits. You’ll need to specify which parent commit you want to maintain the changes from when you revert the revert. Use the following command:

git revert -m 1 REVERTED_MERGE_COMMIT_SHA

Replace REVERTED_MERGE_COMMIT_SHA with the SHA of the reverted merge commit. Again, -m 1 tells Git to favor the changes from the first parent.

Handling Conflicts During Undo

In some cases, when you’re reverting a revert, you may encounter conflicts. Git cannot automatically merge changes and needs your intervention to decide which changes to keep. You will need to manually edit the conflicting files, decide how to resolve the conflicts, and then continue the reverting process.

To address conflicts, start by inspecting the conflicting files:

git status

The output will highlight the files with conflicts. Edit these files, resolve the conflicts, and then mark them as resolved using:

git add <filename>

Once all conflicts have been resolved, you can finish the revert:

git revert --continue

Advanced: Interactive Rebasing

When dealing with a complex commit history or needing to undo a revert amongst many other commits, an interactive rebase may be the best approach. This allows you to edit, remove, or rearrange commits in a more controlled environment. Use the following command to start an interactive rebase:

git rebase -i REVERT_COMMIT_SHA^

You must specify the commit just before the revert commit. During interactive rebase, you will be presented with a list of commits in your text editor. Find the line that corresponds to the revert commit and delete it or replace ‘pick’ with ‘drop’. Then, save and close the editor.

Git will now rebase your commits without the revert commit. Note that this modifies your repository’s history, which could cause problems for others collaborating on the same branch. If you’ve already pushed the changes to a remote repository, you will have to force push using:

git push --force-with-lease

Be sure to coordinate with your team when rewriting history as it may require them to rebase their work as well.

Conclusion

Undoing a Git revert is a powerful action that can help you correct mistakes. Whether using a simple reset, reverting the revert commit, or performing an interactive rebase, you have several tools at your disposal. Always confirm the commit SHA you’re working with, be aware of the implications of history rewriting, and communicate with your team whenever you make significant changes to shared repositories.