DEV Community

Syed Ale Ajwad
Syed Ale Ajwad

Posted on

πŸ” How to Revert a Git Revert


πŸ‘©β€πŸ’» Real-World Context: CPQ Workflows and Spec Change Reverts

In CPQ development, it's not unusual to deploy a Spec Change to UAT, feel proud of your work, and then suddenly hear:

"Can you revert that from PROD?"

Rather than manually deleting or commenting out code (πŸ‘Ž inefficient and error-prone), we do what any self-respecting developer would:

Use git revert to undo the commits cleanly and traceably.

But then comes the real twist:

Later on, you’re asked to bring the changes backβ€”and Git greets you with:

β€œMerge request? What merge request? Nothing changed.”

Welcome to the paradox of the reverted feature branch.


❓ What Went Wrong?

  1. When you reverted the changes, Git added new commits that undo your work.
  2. Your original commits are still in history, so Git thinks your feature is already merged.
  3. Trying to rebase or merge the feature branch again leads to a classic nightmare: 🚨 Conflicts everywhere.

βœ… The Solution: Revert the Revert

Let’s walk through it step by step.


πŸ› οΈ Step-by-Step Guide


Step 1 β€” Find the Revert Commit(s)

You can find the commit hash of the revert commit in a couple of ways:

  • Within Visual Studio: Open the Git history view to see recent commits, including revert commits. Look for commits with messages like:
  Revert "feature/spec-change"
Enter fullscreen mode Exit fullscreen mode
  • In Azure DevOps: Navigate to the Completed Pull Request (PR) that contains the revert. There’s a Commits tab that lists all commits included in that PRβ€”find the revert commit here and copy its commit hash.

Image description

  • Or via Terminal:
  git log
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Branch From the Original Feature

1.Switch to master and update it:

   git checkout master
   git pull origin master
Enter fullscreen mode Exit fullscreen mode

2.Checkout your original feature branch:

   git checkout feature/spec-change
Enter fullscreen mode Exit fullscreen mode

3.Create a new branch to revert the revert:

   git checkout -b feature/revive-spec-change
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Perform Your Preferred Stress-Relieving Activity

Take a deep breath and do what helps you relax β€” walk, grab a coffee, meditate, or if you’re like me,

curse the day you decided to become a developer.


Step 4 β€” Merge Latest Master Into Your Branch

git merge master
Enter fullscreen mode Exit fullscreen mode

This brings in the revert commits so they become part of your feature branch’s history.


Step 5 β€” Revert the Revert

You can do this either via command line or Visual Studio:

  • Using Git CLI: For a revert of a merge commit use:
  git revert -m 1 <revert-commit-hash>
Enter fullscreen mode Exit fullscreen mode

The -m 1 flag tells Git to keep the first parent (usually master).

  • Using Visual Studio: In Git History, right-click the revert commit and select Revert.

Image description


Step 6 β€” Handling Multiple Revert Commits

If your original feature had multiple commits reverted individually, you'll have multiple revert commits.

For example, if the original commits were:

Original Commit Revert Commit
C1 R1
C2 R2
C3 R3

You need to revert these revert commits in descending order (latest first):

git revert <R3>
# If conflicts arise, resolve, commit, then continue
git revert <R2>
git revert <R1>
Enter fullscreen mode Exit fullscreen mode

Reverting them in descending order prevents merge issues that might arise due to dependency between the commits.


Step 7 β€” Resolving Conflicts During Revert

If you encounter conflicts during any revert:

  1. Resolve the conflicts manually in your code editor.
  2. Stage the resolved files:
   git add .
Enter fullscreen mode Exit fullscreen mode
  1. Commit the conflict resolution:
   git commit
Enter fullscreen mode Exit fullscreen mode
  1. Continue with the next revert commit.

Step 8 β€” Important: Don’t Revert Merge Commits

Merge commits like:

Merge branch 'feature/spec-change' into master
Enter fullscreen mode Exit fullscreen mode

Do not revert these. Only revert the actual commits that contain code changes.


Step 9 β€” Push and Create a New Merge Request

Once all revert reverts are done, push your branch and create a PR to merge your changes back in:

git push origin feature/revive-spec-change
Enter fullscreen mode Exit fullscreen mode

Then open Azure DevOps and create a new Pull Request for your branch.


πŸ§˜β€β™‚οΈ Final Tips and Summary

Task Command / Action
Find revert commit Visual Studio Git History or Azure DevOps PR Commits tab
Branch from original feature git checkout -b feature/revive-spec-change
Merge master git merge master
Revert a merge revert commit git revert -m 1 <hash> or Visual Studio Revert
Multiple revert commits Revert in descending order one by one
Resolve conflicts Manual resolve β†’ git add . β†’ git commit
Don’t revert merge commits Ignore these, only revert actual commits
Push and PR git push + create PR in Azure DevOps

Happy coding! πŸš€

Top comments (0)