Dima Lituiev
Dima Lituiev

Reputation: 13136

git: how to merge a file manually when git does not notice conflicts

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?

Upvotes: 3

Views: 109

Answers (3)

jthill
jthill

Reputation: 60605

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.

Upvotes: 2

David Deutsch
David Deutsch

Reputation: 19045

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?

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142662

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?

Upvotes: 1

Related Questions