Search code examples
gitgit-mergegit-merge-conflict

Merging master into feature branch with conflict resolution


Let's say I have a feature_branch that is based on master and contains commits D and E. Afterward, master receives two new commits, F and G :

master:        A---B---C---F---G
                       \
feature_branch:         D---E

When I merge master into my feature_branch to incorporate the new changes, I encounter some conflicts. After resolving these conflicts in the feature_branch, a merge commit is created (let's call it M) in feature_branch. At this point, the history looks like this:

master:        A---B---C---F---G
                       \        \
feature_branch:         D---E---M

Now, here's where I get confused: the master branch still points to commit G, which is the last commit before the conflict resolution occurred in feature_branch.

When I later merge feature_branch back into master, I get a fast forward merge, why don't I encounter the same conflicts again, since the conflict resolution has introduced a different change ? How does Git handle this situation?

I have tried to merge back the feature_branch into branch and expected to get the same conflict again, since the conflict resolution has changed the file but the master branch points to the previous commit that has another version of the file.


Solution

  • You need to digest the way that merges work. When you merge a branch A with B, what is considered for the merge are changes introduced, roughly, after the last common ancestor of A and B (say, this commit: git merge-base A B). When you merged M into master git does not consider changes in both branches from C but from G (the last commit that is present in the history of both branches). If there are no changes from there that would produce a conflict, then there is no conflict. And, also, the fact that there was a conflict at some point in history of the branches is not relevant to git... All it considers are the differences between G..master and G..feature_branch.

    Side note: in this case you had a fast-forward because there was no commit in master after G..... If there had been commits past G, then you would get conflicts depending on the differences between G..master and G..feature-branch.... It might very well be the case that no conflict arises.