views:

88

answers:

2

Hi, i'm just testing git to find out if i could use it for my work. I ran into a problem that seems small but could become a real one with the real code. My file looks like: text.txt 1 2 3 4 I've a local branch "branch1" and commited changes in both branch and master. In master i changed the first line in the branch the second. So the diff for master looks like this:

+1 master
 2
 3
 4

For the branch it is:

 1
-2
+2b1
 3
 4

Running git merge branch1 resolves in a conflict:

<<<<<<< HEAD
1 master
2
=======
1
2b1
>>>>>>> branch1
3
4

I know this one can be resolved easily. But how is this a conflict, anyway. Shouldn't git be able to merge this?

+4  A: 

There's no context that isolates the two changes so it's not clear what the the correct resolution should be. With one line of context the changes are: change the block "1/2" to "1 master/2" and change the block "1/2/3" to "1/2b1/3".

Trying to apply the second 'patch' to the result of the first patch resolves in an error because the context needed to successfully apply the patch is not a match. The patch needs "1/2/3", but has "1 master/2/3".

Sufficient context is important in more complex scenarios as without it, it would be easy for merge to apply the patch in the wrong place without warning if the local branch had moved enough lines around and the minimal amount of context checked at the original location was sufficiently non-specific that the patch still applied when it shouldn't.

Charles Bailey
+4  A: 

Couple of comments:

  • first, such a small example would never be merged anyway:

    warning: Cannot merge binary files: afile.txt (HEAD vs. abranch)
  • then, if you have many "small" merge conflicts which you know should be resolved independently of the context, you could try rebasing first your branch on top of master, ignoring the context:

    git rebase -C0 master

and then merge your branch into master.

This is generally not a good idea to ignore all context for a rebase, but if you are certain about your modifications (as in "modifications needing no context at all"), it will work.

From git rebase man page:

-C<n>

Ensure at least <n> lines of surrounding context match before and after each change.
When fewer lines of surrounding context exist they all must match.
By default no context is ever ignored.


You can test it easily enough (here in a PowerShell session, with Git1.6.5.1, on Xp)

First create a small bat utility genfile.bat

echo hello, World %1 > afile.txt
echo hello, World %2 >> afile.txt
echo hello, World 3 >> afile.txt
echo hello, World 4 >> afile.txt
echo hello, World 5 >> afile.txt

then create a repo and add a file:

PS D:\git\tests\mergeLines> git init m0
PS D:\git\tests\mergeLines> cd m0
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1 2
PS D:\[...]\m0> git add -A
PS D:\[...]\m0> git ci -m "afile to be modified concurrently"

Your file looks like this:

hello, World 1
hello, World 2
hello, World 3
hello, World 4
hello, World 5

Modify it in a branch

PS D:\[...]\m0> git co -b abranch
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1 2_modified
PS D:\[...]\m0> git ci -a -m "afile modified in abranch"

You will have:

hello, World 1
hello, World 2_modified
hello, World 3
hello, World 4
hello, World 5

Then modify it in master

PS D:\[...]\m0> git co master
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1_master 2
PS D:\[...]\m0> git ci -a -m "afile modified in master"

Which gives you:

hello, World 1_master
hello, World 2
hello, World 3
hello, World 4
hello, World 5

Clone that repo for a first experiment (i.e.: a merge of abranch into master)

PS D:\[...]\m0> cd ..
PS D:\git\tests\mergeLines> git clone m0 m1
PS D:\git\tests\mergeLines> cd m1
PS D:\[...]\m1> git co -b abranch origin/abranch
PS D:\[...]\m1> git co master
PS D:\[...]\m1> git merge abranch

That gives you a conflict:

Auto-merging afile.txt
CONFLICT (content): Merge conflict in afile.txt
Automatic merge failed; fix conflicts and then commit the result.

PS D:\[...]\m1> type afile.txt
<<<<<<< HEAD
hello, World 1_master 
hello, World 2 
=======
hello, World 1 
hello, World 2_modified 
>>>>>>> abranch
hello, World 3 
hello, World 4 
hello, World 5

Clone the first repo again, this time to rebase first abranch on top of master, with no context:

PS D:\[...]\m1> cd ..
PS D:\git\tests\mergeLines> git clone m0 m2
PS D:\git\tests\mergeLines> cd m2
PS D:\[...]\m2> git co -b abranch origin/abranch
PS D:\[...]\m2> git rebase -C0 master

Your file is silently merged:

hello, World 1_master
hello, World 2_modified
hello, World 3
hello, World 4
hello, World 5

Off course, if you switch back to master and now merge abranch, the result will be a fast-forward merge.

PS D:\git\tests\mergeLines\m2> git co master
Switched to branch 'master'
PS D:\git\tests\mergeLines\m2> git merge abranch
Updating c8f48b4..8bee1d2
Fast forward
 afile.txt |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
VonC
Thanks for your help, so far. I am just starting with git and have so far heard nothing of the context. (A quick search didn't make it any clearer)Anyway, this is a scenario i'am regularly dealing with, except with more lines of code. Do you suggest, it would work properly with more lines? I have thought about this for some time. I think if Git is so superior this should work.By the way i tried rebase -> same conflict.
Benjamin