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

Time:10-04

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 :)

CodePudding user response:

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

CodePudding user response:

Well...... there are two parts to this process. The first is called "correcting the wrong merge". You can either retry the merge from scratch.... or you can amend the one you did before to correct some mistakes. Let's say that the bad merge is commit X.

First way would be:

git checkout X~
git merge X^2
# correct all conflicts
git add .
git commit -C X # use the same commend as the original X

Second approach is

git checkout X
# do the necessary adjustments by comparing with the parents and so on
git add .
git commit --amend --no-edit

Either way, you are now standing on the corrected X and we have to go into the second step, which is pretty appropriately called "the second step".... we cherry-pick whatever you put on top of X

git cherry-pick X..feature-branch
# if you like the result
git branch -f feature-branch
git checkout feature-branch

And that's it.

  • Related