Revert commits unique to a branch

54 views Asked by At

I have just finished fixing an extremely long merge conflict and then realised I merged to the wrong branch! I merged to dev-majorversion when I meant to merge to dev-majorversion.minorversion. Is there any way I can undo the commits that exist on majorversion but do not exist on minorversion for branch to undo these commits, or will I need to merge again?

1

There are 1 answers

0
VonC On

You could cherry-pick your merge resolution:

git checkout dev-majorversion.minorversion
git cherry-pick <SHA1 of commit resolving merge>
git merge --ours <yourBranchToMerge> # the one your already merge into the wrong branch

The git merge --ours records the merge between <yourBranchToMerge> and dev-majorversion.minorversion, but keeps the content of dev-majorversion.minorversion intact (including its cherry-picked commit).

You would still have to validate that the commit cherry-picked is compatible with the content of dev-majorversion.minorversion (since it represents conflict resolution initially done on dev-majorversion)

If so, you can go back to dev-majorversion and reset HEAD to the previous commit to cancel the merge done here (assuming the merge was the last operation done on dev-majorversion, and that is wasn't pushed yet)

git checkout dev-majorversion
git reset --hard @~