Merge conflict resolution over new code

Let’s say I have some code that is ordered, but the ordering is not a technical requirement.


And then I have two topics that I want to merge in, whose diffs look like:

  • How can I get Emacs to revert all unchanged buffers when switching branches in git?
  • How to get rid of local commits in git?
  • Set UTF-8 display for Git GUI differences window
  • Git squash merge or git force push?
  • how can I do “git pull” automatically when my server startup
  • GIT “stop tracking” not working using source tree
  • TOPIC BRANCH: orange
    + orange

    And also

    TOPIC BRANCH: pear
    + pear

    Is there a way for these two patches to get resolved automatically? It seems to me like it is a merge conflict since they compete for the same new line. A solution I’ve come up with is to reorder one of the changes since the sort order is only a soft requirement (where fruits are actually function definitions).

     TOPIC BRANCH: pear'
     + pear

    So now we can merge orange and pear' together to form:

     _ apple
     p pear
     _ kiwi
     o orange
     _ strawberry

    Are there other ways to resolve this such that the ordering can be kept? I also thought of pear having to downstream from orange such that orange always gets priority and there wouldn’t be a merge conflict anymore. But this is a false dependency since orange and pear are two separate feature branches.

    One could be mainlined into the trunk before the other but that doesn’t address integration branches.

    Edit: It just dawned at me for two hunks that could be kept (additions only I guess?) there could be two merge strategies called “me first” and “you first” such that an ambiguous ordering could be resolved non-interactively between two branches.

  • Delete git master branch on a private server (not GitHub)
  • How do I setup a version control repository on a web hosting account?
  • git — handling frozen content
  • git - diff of current changes before committing
  • Git can't see files installed by composer
  • How to execute a git commit/push from within a rake task
  • 2 Solutions collect form web for “Merge conflict resolution over new code”

    The basic approach is to define a custom merge tool and then use the git attributes feature to tell git to use that custom merge tool for those files.


    1. create a test repository:

      $ git init t
      $ cd t
    2. define a custom merge tool called mymerge:

      $ git config "my custom merge tool"
      $ git config merge.mymerge.driver "cat '%A' '%B'|sort -u >'%A'.tmp && mv '%A'.tmp '%A'"

      The above merge tool concatenates the files, sorts the resulting lines, and then removes duplicate lines. If you don’t want to change the order, replace the above command with a custom script that does what you want. See git help attributes for more information.

    3. tell git that you want to use mymerge when merging any file named foo.txt in the repository:

      $ echo "foo.txt merge=mymerge" >.gitattributes
      $ git add .gitattributes
      $ git commit -m "tell git to use the mymerge merge tool for foo.txt"
    4. make some test data on three branches:

      $ printf 'apple\nkiwi\nstrawberry\n' >foo.txt
      $ git add foo.txt
      $ git commit -m "common ancestor version of foo.txt"
      $ git checkout -b orange
      $ printf 'apple\nkiwi\norange\nstrawberry\n' >foo.txt
      $ git commit -a -m "add orange"
      $ git checkout -b pear master
      $ printf 'apple\nkiwi\npear\nstrawberry\n' >foo.txt
      $ git commit -a -m "add pear"
    5. merge the branches (note no conflicts!):

      $ git checkout master
      $ git merge orange
      $ git merge pear
    6. profit!

      $ cat foo.txt

    If the code that needs to be ordered is in a separate file, you could use a custom merge tool that takes two input files, reads them and outputs their contents, sorted into your result file. You would then tell git to use the custom merge tool for this file. I don’t know enough about git to know if you can set custom merge tools based on file type or regex of some sort though, but it is an idea that might give you what you want.

    Git Baby is a git and github fan, let's start git clone.