How to PR and merge again after reverting PR using Github Revert Button
Asked Answered
D

7

98

Basically I used Github revert button to revert a previous PR for a feature branch into master, then I decided to merge the same feature branch that I reverted earlier, but I was not able to do so. Steps as follow:

  1. PR to merge feature branch to master
  2. Revert PR merge from (master)
  3. Tried to create new PR to merge feature branch to master again.
  4. Got this message:

There isn't anything to compare.

master is up to date with all commits from feature-branch. Try switching the base for your comparison.

Any suggestions on how can I merge feature branch again into master

Deathday answered 9/1, 2015 at 0:41 Comment(0)
L
86

Just revert the revert. So by clicking the revert button you will have created a new PR (your step 2). Once this is merged, you will have the option to revert this, which will create a new branch with all your changes back in. You can then pull this, make changes to it (if needed) and create a new PR. You will lose all the commit messages on Github, but all file changes will still be around. Good to refer to your original branch and reverts in the new PR.

Anything to avoid a complicated rebase or force pushing to master.

Linalool answered 13/2, 2015 at 10:23 Comment(4)
A) rebase is a useful tool, dont't avoid it bc its complicated. B) reverting the revert has caveats (see my response)Disservice
If you want to clean up the ugly branch name revert-123-revert-123.. or similar you could rename the branch with git branch -m <new_name> and then push commits- git push origin -u <new_name> and then delete that ugly branch name git push origin --delete revert-123-revert-123. More info on linuxize.com/post/how-to-rename-local-and-remote-git-branchImbrication
Doesn't work if original PR contained A+B and now you want to merge just A.Idiotism
Since the Github revert button doesn't always work, the alternative is to manually revert the revert commit that Github previously created. Then just push it to a new branch and create a PR. Of course, make sure your master is up-to-date before starting. (note that github creates a single revert commit that contains all commits from the original branch that was reverted. So it's easy to revert it manually)Princedom
R
71

I am writing this answer since I faced this issue and I found the answers here more theoretical than practical. I surfed a little bit more and found the method to tackle this issue. You can find a more detailed answer in the article here.

To solve this problem you have to create a new branch tracking the master and revert the revert commit. Then checkout to feature branch and merge the new branch. Now you can resolve conflicts (if any), commit and create a new PR.

Here are the commands:

# do the needed changes in the feature branch
$ git commit -m "fixed issues in feature-branch'

# create new branch tracking master branch
$ git checkout -b revert-the-revert-branch -t master

# revert the reversion commit
# find it from your git log
# in linux try: 'git log | grep revert -A 5 -B 5'
$ git revert <revert-commit-hash>

# checkout the original feature branch
$ git checkout feature-branch

# merge the revert branch
$ git merge revert-the-revert-branch

# handle merge conflicts and commit and PR
Ramentum answered 1/5, 2019 at 12:19 Comment(3)
@Deathday please mark this as the answer since most are finding this as the useful and practical answer.Ramentum
HI Shanika, that strategy of yours implies that we should still have the feature branch in hands. But what if the feature branch was deleted upon merge (something that almost all VCS plataforms does nowadays)?Schistosomiasis
Doesn't work if commit and revert contained A+B, and you want to apply A.Idiotism
K
8

I know this is old, but if someone need a good answer is here:

After you merge a PR and delete the brach and later revert this merge, you can create a new branch and then revert the revert. Push this to remote repo and create a new PR.

This will create a new PR with one commit named 'revert "revert #123 blabla"` with all your changes on diff.

https://www.tildedave.com/2012/11/24/reverting-a-github-pull-request.html

Knowle answered 15/3, 2018 at 15:20 Comment(0)
U
5
  1. Switch to the master branch and print out the commit logs git log --oneline. Here search the commit that was made for the PR and copy the commit hash code.
  2. Now run git cherry-pick YOUR_HASH_CODE. This will bring that particular commit to top of the head.
  3. Now create a new branch and switch to this newly created branch and push it to git.
  4. Now create a PR to master from this newly created branch.
Urbani answered 26/11, 2021 at 2:3 Comment(0)
D
4

What

You should pull the most recent master, rebase your branch on master and then you should be able to make another pull request.


Why

The reason you can't auto merge back in is because the base of the branch is out of sync with the HEAD of the master branch.

Reverting the Revert can get messy and sometimes lacks transparency.

Furthermore, reverting a revert will prevent other branches with this code from merging correctly.

Lets say you have feature x on master and merged into branch y. then you decide master shouldn't have had feature x merged in yet as it depends on branch y. So, you revert on master. When you try to merge branch x, git-merge command sees the original merge, and happily announces that all is well and branches have been already merged, omitting these commit for feature x, even though you wanted them merged with branch y.

Disservice answered 19/7, 2016 at 19:3 Comment(5)
This answer is insufficient in all but basic cases due to the fact that you'd have to go find your old branch and copy everything into a text editor to make a new one. Anthony's solution is much more transparent, and the only messiness is in the auto-generated github names, which you can modify yourself.Gussy
Why would you have to copy anything into a text editor? Pull master, switch to your branch (the name of which is in the PR so you don't have to "find" anything), pull branch, git rebase master, force push branch. Done.Disservice
didn't work: git checkout master; git pull; git checkout feature; git pull; git checkout -b feature_rebase; git rebase master; git push (longer version); try to create PR on remote git repo - zero filesIdiotism
If nTry to run the rebase with interactive to get an idea of the commits in the rebase.Disservice
@Idiotism I think this approach relies on rewriting the commits from the reverted branch. Therefore, you need to have a new commit on master, so when you rebase reverted-branch on top of it, its commits will be rewritten. (Not 100% sure, didn't try it)Princedom
P
2
  1. Go the the Revert PR and click "Revert" (But don't merge it)
  2. Do git fetch
  3. Do git checkout <name of revert's revert>

All your changes will be there, and when you create a PR, the changes will show.

To be clear - Clicking "Revert" on the Revert PR will give you a name like revert-202-revert-201-originalbranchname. This is the branch you want to edit on!

Protist answered 15/6, 2021 at 17:17 Comment(0)
A
2

Here is what i did.

  1. checkout your feature branch

  2. Rebase with your base branch: This will bring your feature branch to the state of Base branch because your feature branch commits are already a part of the base branch. Your feature branch commits will be aligned below your revert PR commit.

  3. Then revert the commit that reverted your PR. This will bring your feature branch back to its original state but now with a new commit.

  4. push to feature branch and then raise a PR.

Why this works? by rebasing you bring your feature branch in sync with the base i.e. bring it to equal commits as the base- needed to move ahead of base and also get the commit that reverted your changes onto your feature branch

by reverting you reverse the changes that removed your changes and now your changes can be a part of new commit AHEAD of based branch.

Apologize answered 24/6, 2021 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.