An-droid February 2016

Undo bad merge already pushed to origin

Here is the situation :

I have a develop branche and a feature branche, when finishing the feature I want to merge it into develop. Conflicts arise so, I first pull develop into feature to resolve it then commit the modifications BUT :

I commit only the file with the conflict resolved and forget the rest. So when I merge feature into develop it rollback lots of commits...

origin develop>  ----o-c-c-c-o-------m------
                      \       \     /
 local feature>        f-------m-r-o

The commits 'c' are merge with feature but when merging again with develop I forgot them.

How can I solve this ?

EDIT :

I'm not very experimented with git so i'm asking to have an answer more specific to my problem than the everage git reset response

Answers


Ferrybig February 2016

You have made a bad merge.

origin develop>  ----o1-c1-c2-c3-o2---------m2------
                      \           \        /
 local feature>        f1----------m1-r1-o3

You mistake is in commit m1 and the changes propagated further down the chain.

Rewriting history

The cleanest solution to this problem would be rewriting history so everything is is still flowing correctly. However, this breaks history for every, so they need to reset their branch to the new head, only recommended if you seen your mistake before you pushed.

The fix is redoing the merge, then redoing the final merge with the master.

  1. on branch: feature: git reset --hard f1

    We are going back to the state of the branch at commit f1

  2. on branch: feature: git merge o2

    Redo our merge, making sure we aren't discarding our index this time

  3. on branch: feature: git cherry-pick r1

    Include the changes introduced in commit r1

  4. on branch: feature: git cherry-pick o3

    Include the changes introduced in commit o3

  5. on branch: feature: git push -f

    Force push our fixed feature branch

  6. on branch: feature: git checkout develop

    Switch to develop, so we can fix it

  7. on branch: develop: git reset --hard o2

    We are going back to the state of the branch at commit o2

  8. on branch: develop: git merge feature

    Redo the feature to master branch merge, this fixes the whole chain.

  9. on branch: develop: git push -f

    Force push our fixed de


David Deutsch February 2016

Since you have already pushed, the safest thing to do (while on develop) is to cherry pick the lost commits. For each commit C, run git cherry-pick C.

Post Status

Asked in February 2016
Viewed 1,063 times
Voted 6
Answered 2 times

Search




Leave an answer