Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Revert a Revert for a Merge

I had a feature branch created, let's say feature/branch1 on github. I created a pull request for it and got it merged. Initial PR Merge Commit

When it reached our pipeline, we figured there was a problem and we got it reverted using the Revert button on Git This created a "Revert" PR that we merged with the master and all was well. Reverted Merge Commit

After a few weeks, post other PRs that got merged into the master, we figured we would revert-the-revert. This time, we went into the Revert PR that was closed and tried to use the Revert button again. But we got this error message

Sorry, this pull request couldn't be reverted automatically. 
It may have already been reverted, or the content may have changed since it was merged.

How do I get this revert done?

The most ideal situation I would like to have, is to have a new branch that contains the revert of the revert, so I can make further changes and go back through the PR process.

like image 691
Serendipity Avatar asked Apr 05 '17 04:04

Serendipity


2 Answers

The error which you see is artificial check of github, which I personally find unneeded. You can revert the revert locally then:

git fetch origin master
git checkout origin/master (or reset)
git revert <REVERT HASH>
git push origin master

This should succeed, modulo conflicts with changes done since the revert.

PS: actually, the error could be because of the conflicts.

like image 145
max630 Avatar answered Oct 16 '22 20:10

max630


What you can try is:

  • reset (git reset --hard old_commit) that PR branch to the commit you want to revert to (the one that was reverted)
  • force push (git push --force) that branch: that will update the PR

That way, the PR is done again with the old commit.

This is a merge commit. The PR is already closed and merged.

In that case, if you have fetch that old PR branch, you can do:

  • a git log on it (git log origin/old_pr_branch)
  • a new branch from the old SHA1 commit representing the content you now want

    git checkout -b new_pr_branch old_sha1
    
  • a push to origin

    git push -u origin new_pr_branch

You can then make a new PR from that new branch, with the right content.

like image 33
VonC Avatar answered Oct 16 '22 20:10

VonC