1

Would it be possible to execute a git merge of 2 files with conflicts on only certain lines without having to resort to human input?

See below for example, is there some option flag for git merge that can complete this without having to require additional input from the user?

test1

line1

test2

line2
line3

test2 after merge

line1
line3
Peter Lang
  • 51
  • 5
  • 2
    No, there isn't. Git cannot know what your intent is there, or why keeping `line1` and `line3` is the right way to resolve that conflict. – meager Apr 05 '19 at 16:42
  • `git rerere` _might_ help you. – eftshift0 Apr 05 '19 at 16:43
  • 1
    “Would it be possible to execute a git merge of 2 files with conflicts on only certain lines without having to resort to human input?” if there were, it wouldn’t be a conflict. After all, there are built in strategies already. If none of those suits, that’s that. – matt Apr 05 '19 at 16:47
  • 1
    As @matt said, any possible resolution would have been done. In Git, a conflict does not mean a problem, it means that Git is telling the user: "Hey, I did what I could to merge these changes, but there are some I am not sure about, and I need your help with it". – padawin Apr 05 '19 at 16:51

2 Answers2

0

From: Automatic merge conflict resolution


Like the comments mentioned, Git is only so good at deciding how to put code together. You might be interested in merge strategy options though. You can run

git merge --strategy-option theirs or git merge --strategy-option ours

To favor either the source or the destination when it comes to conflicts. Read more about it here:

Resolve Git merge conflicts in favor of their changes during a pull

https://git-scm.com/docs/merge-strategies

Mike Faber
  • 463
  • 7
  • 16
  • Note that using this may cause unexpected disappearance of changes. I suspect many [complains](https://stackoverflow.com/search?q=%5Bgit%5D+merge+discards+changes) about merges discarding changes can be attributed to the options. The safest option would be the [union driver](https://git-scm.com/docs/gitattributes#Documentation/gitattributes.txt-union) which would most probably break any code but should be ok for human-readable text – max630 Apr 08 '19 at 15:53
0

For a language with known structure, you could try to decompose it to smaller items than lines and get rid of some conflicts. There are some proprietary solutions which are advertised to do it (I have not tried them).

But for many conflicts it is not possilbe to solve them even with that approach. For example, user1 adds a like, and user2 adds another line to same place. Which one should go first? It is something only human can decide.

max630
  • 7,348
  • 2
  • 24
  • 49