Forcing 'git merge' to declare all differences as a merge conflict

前端 未结 3 879
一生所求
一生所求 2021-02-09 19:20

In a \'git merge \' I would like any difference, even if not normally a merge conflict, to be considered a merge conflict. Then, with \'git mergetool\' I can see and resolve ea

相关标签:
3条回答
  • 2021-02-09 19:30

    I have a similar desire and have been unable to do it with the automatic tools. Here is a semi automated solution:

    From master, merge add-on and manually resolve changes:

    Start with:

    git merge --no-ff --no-commit add-on
    

    Then, for each changed file do:

    git show master:path/to/file > from_master
    git show add-on/to/file > from_add-on
    kdiff3 --qall from_master from_add-on -o path/to/file
    git add path/to/file
    

    Finally,

    git commit
    

    Why this 90% solution?

    This solution merges even in "obvious situations" and it doesn't force me to set .gitattributes so I can make "manual merge" behavior dependent on who I am merging from and why rather than on which files I am merging.

    0 讨论(0)
  • 2021-02-09 19:39

    You didn't merge. Your command git merge add-on executed a "fast-forward", which means it just moved the branch head. This is because your add-on branch was descended from the tip of your master branch already, so no merge was needed. If you run git log you'll see there's no merge commit.

    Basically, it looked like this before the merge:

                  master
                 /
    o---o---o---o           add-on
                 \         /
                  o---o---o
    

    and the merge just moved the master pointer to the end of the line:

                              master, add-on
                             /
    o---o---o---o---o---o---o
    

    If you want to force a merge, pass the --no-ff flag, as in git merge --no-ff add-on.


    Upon further reflection, the merge attribute won't do what you want. This only applies to file-level merges, which means both sides of the merge has changes to the specific file. If only one side has changes (which is your case), no file-level merge is done and the changed file is accepted unconditionally.

    Your best bet is probably to use git merge --no-ff --no-commit add-on to produce the merge but don't actually commit. You can now inspect the results and tweak them to your satisfaction before committing the merge. If you want to accept changes on a per-hunk basis you can do something like git reset to reset the index and then git add -p to do per-hunk staging.

    0 讨论(0)
  • 2021-02-09 19:50

    You may want to try:

    git merge --no-commit
    

    Which will make GIT not commit the merge changes. Alternatively if you want it to commit when there are no conflict, but not to lose the source branch, it's no-ff:

    git merge --no-ff
    

    Both can be used if necessary.

    More info here: Why does git fast-forward merges by default?

    0 讨论(0)
提交回复
热议问题