Git Revert a Revert for a Merge

2020-06-10 15:59发布

问题:

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

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.

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.

回答1:

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.



回答2:

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.