Git merge disappointment

Chanoch (Ken) Bloom kbloom at gmail.com
Sun Apr 25 04:16:20 CEST 2010


On Fri, 2010-04-23 at 19:31 +0100, chombee wrote:
> I'd like to keep a text file in my homedir called 'notes' which contains
> all my notes to myself, and track this file and sync it between
> computers using git. I write the file in markdown syntax and each new
> entry usually begins with a header. So on machine A I add a new entry to
> the top of my notes file:
> 
>     # A new entry written on machine A
> 
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
> 
>     # An old entry
> 
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
> 
> (Imagine the A's and C's being the contents of some notes, obviously.)
> On machine B I also add a new entry, so both machines have modifications
> to the file and a merge is going to be needed at some point:
> 
>     # A new entry written on machine B
> 
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
> 
>     # An old entry
> 
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
>     CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
> 
> When I pull from machine B to machine A or vice-versa the merge fails
> and git leaves the file looking something like this:
> 
>     <<<<<<< HEAD:notes.txt
>     # A new entry written on machine A
> 
>     ======
>     # A new entry written on machine B
> 
>     >>>>>> f228....
> 
>     <<<<<< HEAD:notes.txt
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     ======
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     >>>>>> f2281
> 
>     <<<<<< HEAD:notes.txt
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     ======
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     >>>>>> f2281
> 
> ..and so on. Each entry A and B contains multiple paragraphs and git
> interleaves the paragraphs of the two entries together, leaving a
> complete mess for me to untangle. It's the worst thing git could
> possibly do. If there are more than two entries to be merged at once
> it's even worse.
> 
> Git is trying to merge commit A (which adds entry A to the top of the
> notes file) and commit B (which adds entry B). They are two simple
> commits, each one adds just one hunk of text to the file. I'd expect git
> to put all the text of entry A followed by all the text of entry B and
> then ask me to finish the merge, which would be trivial. But instead it
> interleaves them?

I have a similar problem with my main BibTeX database. In BibTeX, you
have a bunch of multi-line text records in the format, similar to the
following:

@article{lingcog/hltnaacl07,
   author="Kenneth Bloom and Navendu Garg and Shlomo Argamon",
   title="Extracting Appraisal Expressions",
   journal="Proceedings of Human Language Technologies/North American 
   Association of Computational Linguists",
   year="2007",
   url="http://lingcog.iit.edu/doc/bloom_naacl2007.pdf"
}

I haven't seen them get interleaved in a merge, but it is annoying to
have merge conflicts if I add two entries to the bottom of the file.

Since the order of the entries in the BibTeX file doesn't matter (each
is identified by its key), I found a very simple solution: every time I
add a new entry, I add it at a *random* location somewhere in the middle
of the file. This way, two machines don't have additions in the same
place, so git sees them as different changes, and merges them
automatically without conflict.

--Ken



More information about the vcs-home mailing list