How to make a Git merge operation ignore identical

2019-02-16 09:30发布

问题:

Lets assume that we have one development branch 'A', and two sub branches 'B1' and 'B2', (both taken from A). Lets say that you run a format code command (in our case ReSharper's cleanup code) on the entire project in B1 and B2.

Now, when we try to merge B2 into B1, Git will report conflicts on all files in the project (quite a large number in our case). When looking closer at each conflict, it seems like Git thinks theres a conflict even though the exact same change was made in both B1 and B2(?)

Is there a way, custom driver/git attribute etc that will make a Git merge operation not report a conflict if the files in B1 and B2 are exactly the same?

Maybe I've got it wrong, maybe it is a whitespace/line ending issue (eg different line endings in B1 and B2) in which case I can find the solution here on stack overflow.

回答1:

Yesterday I had similar issue after a lot of cherry-pick from (and to) a side branch where I after merged with my main branch. A lot of conflicts with identical change. I solved this with a merge strategy:

git checkout main-branch
merge --no-commit -s recursive -X ours side-branch

you can change "ours" to "theirs". Be careful because all conflicts are automatically solved choosing "ours" or "theirs" side. In my case I had few mis-merges due to this, and I fixed manually. See others interesting options of merge strategies here: https://www.kernel.org/pub/software/scm/git/docs/git-merge.html

You can also try to use a rebase (in my case it did not work well because the branchs were too different and I had a new conflict in each new rebase interaction). See this: http://davitenio.wordpress.com/2008/09/27/git-merge-after-git-cherry-pick-avoiding-duplicate-commits/