How to Restore a Reverted Git Commit

John Wachira Mar 14, 2025 Git Git Revert
  1. Understanding the git revert Command
  2. Method 1: Using Git Revert with Commit Hash
  3. Method 2: Cherry-Picking the Reverted Commit
  4. Method 3: Resetting to a Previous Commit
  5. Conclusion
  6. FAQ
How to Restore a Reverted Git Commit

When working with Git, it’s not uncommon to make mistakes or need to undo changes. Sometimes, you might find yourself in a situation where you’ve reverted a commit, only to realize later that you actually need that change back. Fortunately, restoring a reverted commit is entirely possible.

In this article, we’ll explore various methods to un-revert a Git commit, ensuring you can recover your lost work efficiently. Whether you’re a seasoned developer or a newcomer to Git, you’ll find these techniques helpful for managing your version control effectively.

Understanding the git revert Command

Before diving into the methods of restoring a reverted Git commit, it’s essential to understand what the revert command does. When you execute the git revert command, Git creates a new commit that undoes the changes made by a previous commit. This means that while the original commit remains in the history, its effects are negated by the new commit. The good news is that you can easily restore the original commit by referencing it directly.

Method 1: Using Git Revert with Commit Hash

One of the simplest ways to restore a reverted commit is by using the git revert command again, this time targeting the commit that was originally reverted. Here’s how to do it:

git revert <commit-hash>

Replace <commit-hash> with the hash of the commit you want to restore. You can find this hash by running git log and looking for the commit you need.

Output:

[master 1234567] Revert "Your original commit message"
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example, running the git revert command with the original commit hash creates a new commit that reintroduces the changes from that commit. This method is straightforward and keeps your commit history intact. It’s particularly useful when you want to maintain a clear record of what changes have been made over time.

Method 2: Cherry-Picking the Reverted Commit

Another effective way to restore a reverted commit is by using the git cherry-pick command. This command allows you to apply the changes from a specific commit to your current branch. Here’s how to do it:

git cherry-pick <commit-hash>

Again, replace <commit-hash> with the hash of the commit you wish to restore.

Output:

[master 1234567] Your original commit message
 1 file changed, 1 insertion(+), 1 deletion(-)

When you execute this command, Git takes the changes from the specified commit and applies them to your current branch. This method is beneficial if you want to bring back a commit without creating a new revert commit. However, be cautious: if there are conflicts between the changes in the reverted commit and the current state of your branch, you’ll need to resolve those conflicts before completing the cherry-pick.

Method 3: Resetting to a Previous Commit

If you want to completely discard the revert commit and return your branch to the state it was in before the revert, you can use the git reset command. This method is more drastic and should be used with caution, especially in shared repositories. Here’s how to do it:

git reset --hard <commit-hash>

Make sure to replace <commit-hash> with the hash of the commit just before the revert.

Output:

HEAD is now at 1234567 Your original commit message

Using git reset --hard will reset your branch to the specified commit, discarding any changes made after that point. This method is effective if you are sure that you want to remove all subsequent commits, including the revert. However, be aware that this command will permanently delete any uncommitted changes in your working directory, so always double-check before executing it.

Conclusion

Restoring a reverted Git commit is a straightforward process, whether you choose to revert it again, cherry-pick the original commit, or reset your branch entirely. Each method has its advantages and is suitable for different scenarios. By understanding these techniques, you can manage your version control more effectively and recover lost changes with confidence. Remember to take care when using commands that alter your commit history, especially in collaborative environments. With these skills, you’ll be better equipped to navigate the complexities of Git.

FAQ

  1. How can I find the commit hash for a reverted commit?
    You can find the commit hash by running the command git log, which lists all commits in your repository along with their hashes.

  2. Is it safe to use git reset –hard?
    Using git reset –hard is safe only if you are sure you want to discard all changes made after the specified commit. Always ensure you don’t have uncommitted changes you want to keep.

  3. Can I restore multiple reverted commits at once?
    Yes, you can restore multiple reverted commits by executing git revert or git cherry-pick for each commit hash you want to restore.

  4. What happens to the commit history when I revert a commit?
    When you revert a commit, Git creates a new commit that negates the changes of the original commit, keeping the history intact.

  5. Is there a way to undo a revert without losing my changes?
    Yes, you can use git cherry-pick or git revert again to restore the changes from the original commit without losing any work.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Revert