Search code examples
gitmergegitlabmerge-conflict-resolution

Fixing bad conflict resolution after wrong merge from master into my feature branch


I have a feature branch only I'm working on.

I made a merge from master into my feature branch, to keep it up-to-date, and accidentally solved conflicts wrong.

Kept working on my feature and pushed more changes. How can I revert things to: not bring in the bad "resolved conflicts" into my feature branch?

Essentially what I want to do is to make sure I am in my current branch in the following state:

1/ exactly as I was before I did the bad merge conflict resolution ;

And

2/ somehow I want to be able to, from the state I'll be after doing 1,still apply the changes I've done after my bad merge, maybe using a cherry pick on each individual commit or something...

I read that maybe an interactive rebase can be what I need but I've never done it.... Any help appreciated.

I tired to do a git revert <SHA1 of the merge commit> that I saw in git reflog but that didn't work as I need to pass the parent ID commit with the -m flag but doing that didn't revert what I was expecting.

What I think I want to do now is: go back to one commit right before I did this bad merge from master into my feature branch and then I want to interactively cherry pick which commits to apply to this new current state as if I can do that, I can then "skip" over the bad merge commit. Obviously I'll then have to merge master into the branch again but then I'll be careful :)


Solution

  • First, the best practice is, if you are the only one working on feature branch, to rebase feature branch on top of master when you want to update it with master content.
    Not to merge master to feature.

    You merge topic branches to integration branches (like master), not the opposite. That way, you can "move around" (rebase/replay) your feature branch without bringing with said feature branch a huge merge commit from the integration branch (master, which, as an integration branch, brings a all lot of other branches).

    Second, you can git reset --hard your feature branch to before the merge, redo the merge, and then replay (rebase) your subsequent commits

    git switch feature
    
    # mark what the old history looked like before reset
    # First feature branch as tmp
    git branch tmp
    # Then the bad merge commit
    git tag merge-commit <sha1-merge-commit>
    
    # reset your branch to before the bad merge commit
    git switch -C feature merge-commit~
    # redo the merge
    
    # Then restore your work
    git rebase --onto feature merge-commit tmp
    git switch feature
    git merge tmp
    
    # cleanup
    git branch -f tmp
    git tag -d merge-commit
    
    git push --force feature