klode
klode

Reputation: 11071

how to reset after merging a git PR by mistake

By mistake I merged a git pull request (someone else has to merge it, not me...) how can I roll back and create the pull request again

this is what I did:

1) created a pull request to merge branchA into branchB (PR #44)

2) (by mistake) merged the pull request #100 (on GitHub site)

3) Clicked the Revert button to "create a new pull request to revert these changes" (PR #45)

4) Merged pull request #45

Now I am trying to again create a pull request to merge branchA into branchB but it does not let me because "There aren't any commits to merge" (I am using git desktop gui on Mac)

Should I reset branchB to the last commit before action (1) ?

And then create a pull request to merge branchA into branchB ?

If yes, how? Would this be correct?

When on branchB:

git reset --hard 4f5oo77

git commit -am "reset to the last commit before merging PR 44"

git push origin

EDIT: this is the log history on branchB

I wan to go back to commit "4f5oo77 2015-12-01 | Merge pull request #36 from my-repo/branchA"

*   87a8888 2015-12-16 | Merge pull request #45 from my-repo/revert-44-branchA (HEAD, origin/branchB, branchB)
|\  
| * 55bd11c 2015-12-16 | Revert "merge branchA" (origin/revert-44-branchA) 
|/  
*   y77447a 2015-12-16 | Merge pull request #44 from my-repo/branchA 
|\  

....

| |/  
* |   4f5oo77 2015-12-01 | Merge pull request #36 from my-repo/branchA
...

EDIT-2 This is what ended up doing:

git checkout branchB
git reset --hard 4f5oo77
git push --force-with-lease

At this point branchB was back to its "state" before the unwanted merges and I was able to create a pull request for merging branchA into branchB

Upvotes: 1

Views: 1357

Answers (3)

morxa
morxa

Reputation: 3374

According to this blog post about undoing merges, the best way to solve your issue is:

  1. Revert the commit which reverts the merge (in your case: git revert 55bd11c)
  2. Merge the branch again to get any additional commits which were done after you merged the first time.

This should add all commits from the merged branch without doing any rebasing and/or rewinding.

Upvotes: 0

Philippe
Philippe

Reputation: 31187

Except if you REALLY can't modify an history that you already shared (pushed), I think that it's much better to use 'git reset' and 'git push --force-with-lease' instead of ditching your history by stacking revert commits!

Upvotes: 1

Ajedi32
Ajedi32

Reputation: 48418

The problem here is that the commits in branchA are already in branchB (even though they've been reverted by a later commit), so GitHub thinks there's nothing to merge.

At first I thought perhaps you could solve this with git rebase, but in reality even git rebase will ignore commits that it thinks are already in the branch you are rebasing to. (Surprisingly this is true even when you use the --no-ff option, which is supposedly meant to prevent this.)

So instead, you can cherry-pick the commits from branchA onto a new branch (such as branchA-new), then use that branch to send the PR:

git checkout -b branchA-new branchB # Create a new branch off of branchB
git cherry-pick 4f5oo77..branchA # Copy the changes from branchA onto the new branch

From there, you should be able to push branchA-new up to GitHub and submit a new PR for those changes.

Upvotes: 0

Related Questions