0

I merged a pull request(will be referred as PR) in GIT accidentally. Now i want to undo the merge.

7b10185a28e09a74e395099435304852f0bb1a1d is the previous merge commit
5d8f83671ce03b1a3d181ec13e836833afa7b77c is the accidental merge commit

I checked out the branch that i merged the PR.

git checkout dev
git pull
git reset --hard 7b10185a28e09a74e395099435304852f0bb1a1d
git checkout -b my-new-branch
git push origin my-new-branch

and then try raising a PR to dev branch, i dont see any difference. Kindly help.

Thanks in advance

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • did you already push the change to dev on remote – Shubham Srivastava Aug 26 '20 at 10:46
  • The easiest way, _if_ nobody else has pulled the faulty commit yet, is `git checkout dev`, `git pull`, `git reset --hard 7b10185`, `git push --force origin dev`. That just gets rid of the merge commit entirely, instead of trying to revert it (which is tricky business, sadly, for reasons described in [this answer](https://stackoverflow.com/a/7101376/14637)). – Thomas Aug 26 '20 at 10:48
  • Here is answer: https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch – Onur Demir Aug 26 '20 at 10:55

2 Answers2

0

When you run git log, you will see:

commit 23b86da331a5279efc541394d3adcbeb27aab3f1 (HEAD -> master)
Merge: d7cf911 ce96497
Author: Onur Demir <xx@x.com>
Date:   Wed Aug 26 13:54:47 2020 +0300

    Merge branch 'feature1'

Formula: git revert <merge_commit_hash> -m <parent_number>

Example:

You can run git revert 23b86da331a5279efc541394d3adcbeb27aab3f1 -m 1

Here 1 is referring to parent d7cf911, 2 is referring parent ce96497. Choose the one which you want to keep.

Onur Demir
  • 708
  • 2
  • 13
  • 35
0

Thanks for your suggestion Onur Demir, i was able to revert it.

git checkout dev

git checkout -b my-new-branch

git revert -m 1 5d8f83671ce03b1a3d181ec13e836833afa7b77c

git push origin my-new-branch

and raised PR, this helped.

But when i try to raise a PR once again from the branch that was merged accidentally, i don't see any difference :(