3

I am working on a collaborative project and often files get conflictlessly merged when I wish they raised conflicts and I could merge them manually. Specially when people revert changes.

       B1 ------C1
      /            \
A --- B  --- C ---- D -- 

So changes in D get overwritten by old stuff from B.

Is there a way to force git merge a specific file specifying it has conflicts?

Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
  • 2
    BTW, even if there is no conflict and you still want to stop the auto merge, review changes and then merge, you could use `git merge --no-commit --no-ff`. This would allow you to verify changes and then commit manually. – Royal Pinto Feb 14 '16 at 07:21

3 Answers3

2

Yes.

echo path.pattern -merge >>.gitattributes

git attributes docs

where path.pattern is as for gitignore and the like.

This will avoid any attempt at resolving potential conflicts, but as David Deutsch points out in comments, if file.txt changed in B..C1 and didn't change in B..C, git will always take the C1 version - git doesn't regard the absence of any change in one tip as something to be considered as potentially conflicting with whatever changes are present in another.

For the really out-there cases like that, where the important part is that a file didn't change, you have to use --no-commit and fix up your index manually.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Note that you will want to use `>>`; a single `>` will overwrite the entire contents of the file. – David Deutsch Feb 14 '16 at 16:56
  • Also, it seems like this will only work if the file was changed in some way in `B..C`; otherwise it just gets overwritten with the copy from the branch. – David Deutsch Feb 14 '16 at 17:04
  • the `-` part of the `-merge` is the `unset` of that attribute for your chosen path pattern (e.g. `*`) to say do not merge paths of this type - see the example definition of the `binary` macro (all within the `git help attributes` doc). Further, that results in the three way merge semantics being "Take the version from the current branch as the tentative merge result, and declare that the merge has conflicts. This is suitable for binary files that do not have a well-defined merge semantics." – Philip Oakley Feb 14 '16 at 23:03
  • @DavidDeutsch thanks for the `>>`, applied, and for pointing out that git doesn't regard the absence of any change as something to be considered as potentially conflicting with whatever changes are present. – jthill Feb 14 '16 at 23:05
2

One thing you can do in this situation is to pass --no-commit to git merge, then go through the files you want to change before committing the merge. However, I would recommend merging as normal, and then doing another explicit commit that restores the files you want. Making unnecessary changes to files in the middle of a merge is rarely a good idea, as it can lead to confusing histories. For example, if you "fix" the problem in the middle of a merge, then later on do a log of the file in question, you won't see the fix, as logging a file history often skips merges. Note that with either approach, if your branch is ever merged back into that other branch, that other branch will have the "fix" as well.

That being said, it seems like there is a deeper problem here. If somebody reverted B, I assume they did so for a reason, and that their changes in C1 depend on that. So do you really want to be merging in C1 without also merging in their revert? Or was the revert a mistake, in which case you need to educate the developer who did it to stop doing such things?

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
1

Is there a way to force git merge a specific file specifying it has conflicts?

Nope.

Git trace changes and once git does not "recognize" changes you will not get conflicts.

Git use heuristics https://www.kernel.org/pub/software/scm/git/docs/technical/pack-heuristics.txt to track changes. So unless there is a real conflict (example: same line changes) there will be no conflicts.

If you wish to know how git calculate the diff read this:
What is the diff version git use? diff2 or diff3?

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167