jameszhao00
jameszhao00

Reputation: 7301

When will merge fail?

I've been using SCMs (SVN, Git) for a while and their merge robustness' always been puzzling. Sometimes automated merges work, and sometimes they don't. Are there general rules on what will automatically merge and what won't?

Upvotes: 0

Views: 354

Answers (3)

Medo42
Medo42

Reputation: 3821

Very generally, a merge will automatically succeed if the SCM can figure out by itself how to create a new version that contains all the changes from all the branches that you are merging, or at least if it thinks that it can figure it out.

If a file is only changed in one branch but not the other, the solution is obvious: Take the changed file. Things only become interesting when a file has been changed in different ways in two different branches, because then the SCM needs a way to combine these changes.

Now to become much more specific and practical, most SCMs can only merge text files. Let's take an image file as example: Maybe two different, non-overlapping parts of the image have been changed in two different branches. In theory, it would be possible to merge this automatically, but most SCMs don't have an algorithm for that, so they will simply show you a conflict with the entire file.

So, what about text files? Here, the SCMs do have algorithms for combining changes. These usually view lines as the basic working units. Speaking a bit simplified, a merge will succeed if the lines changed in the first and the second branch don't overlap. It will fail if they do, i.e. if the same lines (or adjacent lines) have been changed in both branches. As I said, the truth is a bit more complicated, but this should do to get the general idea.

Finally, you should also be aware that an automatic merge can be wrong even if it succeeds without a problem. For example, consider the sentence "Git can merge" as the data in your repository. Now, branch A changes it to "Git can bisect", and branch B changes it to "CVS can merge". The two changes are in different places, so you could theoretically merge them automatically. As I said above, most SCMs wouldn't because it's in the same line, but it's only an example :). So, merging the two branches would result in "CVS can bisect", which is obviously wrong. This kind of problem doesn't happen very often though.

Upvotes: 1

Mike Valstar
Mike Valstar

Reputation: 3519

Essentially a merge will fail when a line is edited that was also edited by someone else or they edited an adjacent line. Git is a little better then subversion for resolving merge conflicts. To help avoid conflicts commit as often as possible (on each atomic change you make to the source is ideal).

Also see Resolving a Merge Conflict

Upvotes: 1

billygoat
billygoat

Reputation: 21984

I think the problem is a certain lack of understanding of how version management work. Version Management is no panacea, it cannot read minds. So if two people are collaborating on a file and if there are conflicting changes to a portion of the file, then the system (git.. Which probably has the best merging capabilities) will not know, which version to keep . ( It does not prioritize ones changes over another), so this needs manual intervention.

This is not an limitation with the version control mechanism, but is a very critical functionality. Sometimes when you have conflicting changes done on the same piece of code, the system should not merge. (since changes made by one of the two collaborators will be lost).

Upvotes: 1

Related Questions