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?
Couple of comments:
first, such a small example would never be merged anyway:
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: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:
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
then create a repo and add a file:
Your file looks like this:
Modify it in a branch
You will have:
Then modify it in master
Which gives you:
Clone that repo for a first experiment (i.e.: a merge of
abranch
intomaster
)That gives you a conflict:
Clone the first repo again, this time to rebase first
abranch
on top ofmaster
, with no context:Your file is silently merged:
Off course, if you switch back to
master
and now mergeabranch
, the result will be a fast-forward merge.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.