"<<<<<<< HEAD" in the middle of the code after merging

Asked

Viewed 495 times

0

I went to make merge between the branch develop and master and some conflicts appeared, but I ended up solving it. The problem is that I saw two files in which have <<<<<<< HEAD in the middle of the branch master. I ended up removing this part manually, but as were several affected files on merge, I think there might be a few more with this problem.

I thought that in doing merge again this would tidy up, for the develop does not have these errors, then delete from master, but does not change anything (only new changes).

Would there be any way to fix it? Or even at the time of doing merge replace all files from master for develop?

1 answer

3

This is the indicator of conflict. If you give a git status you may find that you are in the middle of a conflict resolution yet. Or if you are not... it means you have made a mess. It means that you committed a "conflict resolution" that included this marker (along with the ======= and the >>>>>>>>> develop).

Git only makes this mark when it can’t turn itself around to solve the changes. The case where git doesn’t know how to handle this is when there are two changes in the same context.

For example, imagine I have this C-simile code:

if (num % 2 == 1) 

So in the develop, someone alters to:

if (num % 2)

But I, in the master alter to:

if (num & 1)

How will git proceed? It will identify that the common basis of the two codes was:

if (num % 2 == 1)

Note for the next paragraphs: code line prefixed with - indicates line removed, already line prefixed with + indicates line added

That the develop is the following operation:

-    if (num % 2 == 1)
+    if (num % 2)

Is that the change in master is the following operation:

-    if (num % 2 == 1)
+    if (num & 1)

This means that there were two different actions

-    if (num % 2 == 1)
+    if (num % 2)

and

-    if (num % 2 == 1)
+    if (num & 1)

for the same context (original line if (num % 2 == 1)). The most information git can get from here is that the original line has been removed, but it won’t know what action to take. For this, it will generate more or less the following after the merge attempt (git merge develop):

<<<<<<<<<< HEAD
    if (num & 1)
==========
    if (num % 2)
>>>>>>>>>> develop

With this, it’s up to the human to decide what to do with it and then, and only then, to communicate to git that the conflict has been resolved. This case of conflict is resolved by git add meu/arquivo/conflito.c to list each conflict resolution, then confirmed with a git commit traditional.

This is by far the most common conflict case I have in developing in the company where I work. There are other conflict cases, with changes in the same context, like, I changed the file but Articuno decided to delete it in his branch. When trying to merge, git will complain that there was a "change/remove conflict".

Sometimes the conflict does not imply that both heads have in fact erased the same line and inserted its change. I have had such conflict:

Base file:

int x;
int a;

Head patch-1:

int x;
-int a;
+int a = 2;

Head patch-2:

int x;
int a;
+char *palavra;

In this case, the post-merge would be something similar to this:

int x;
<<<<<<<<<< HEAD
int a = 2;
==========
int a;
char *palavra;
>>>>>>>>>> patch-2

Note that here git was confused as to whether or not to remove the line int a;, because in one of the branches it really was deleted, while in the other I just attached to it.

  • git status shows you have nothing to commit to. Now to remove this only manually?

  • @Have you ever pushed the changes? If yes, you would be able to make one force push? I would recommend rewriting the history and removing that merge, then performing a new merge and only finishing it when it is all duly settled, and push after that. Now, I don’t think I made it very clear, but this merge process, the moment git detects changes in the same context, is whole manual, and should at all times be resolved by a human. If this is in the archive, it is a sign that the human has not properly resolved the conflict at merge time.

  • You can do a normal commit with text edits removing these lines, but it’s in the history of this shame forever

  • Yes, the changes are already in production. It was stupid of me. Because of the errors, I removed manually the files I found. And now I’m looking for one by one to see if I missed any with this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.