Types of merges
There are two types of merges: automatic and manual.Unstructured merge
Unstructured merge operates on raw text, typically using lines of text as atomic units. This is what Unix tools (diff/patch) and CVS tools (SVN, Git) use. This is limited, as a line of text does not represent the structure of source code.Structured merge
Structured merge tools, or AST merge, turn the source code into a fully resolved AST. This allows for a fine-grained merge that avoid spurious conflicts. Yet, it also creates a new problem: as the AST abstracts away formatting, the pretty-printing back from AST to source code may result in a completely different formatting style on the merged files. To overcome this problem, it is required to do some high-fidelity pretty-printing, which is essentially language-specific.Workflow
Automatic merging is what version control software does when it reconciles changes that have happened simultaneously (in a logical sense). Also, other pieces of software deploy automatic merging if they allow for editing the same content simultaneously. For instance, Wikipedia allows two people to edit the same article at the same time; when the latter contributor saves, their changes are merged into the article instead of overwriting the previous set of changes. Manual merging is what people have to resort to (possibly assisted by merging tools) when they have to reconcile files that differ. For instance, if two systems have slightly differing versions of a configuration file and a user wants to have the good stuff in both, this can usually be achieved by merging the configuration files by hand, picking the wanted changes from both sources (this is also called two-way merging). Manual merging is also required when automatic merging runs into a change conflict; for instance, very few automatic merge tools can merge two changes to the same line of code (say, one that changes a function name, and another that adds a comment). In these cases, revision control systems resort to the user to specify the intended merge result.Merge algorithms
There are many different approaches to automatic merging, with subtle differences. The more notable merge algorithms include three-way merge, recursive three-way merge, fuzzy patch application, weave merge, and patch commutation.Three-way merge
Recursive three-way merge
Three-way merge based revision control tools are widespread, but the technique fundamentally depends on finding a common ancestor of the versions to be merged. There are awkward cases, particularly the "criss-cross merge", where a unique last common ancestor of the modified versions does not exist.Fuzzy patch application
A patch is a file that contains a description of changes to a file. In the Unix world, there has been a tradition to disseminate changes to text files as patches in the format that is produced by " diff -u". This format can then be used by the patch program to re-apply (or remove) the changes into (or from) a text file, or a directory structure containing text files. However, the patch program also has some facilities to apply the patch into a file that is not exactly similar as the origin file that was used to produce the patch. This process is called ''fuzzy patch application'', and results in a kind of asymmetric three-way merge, where the changes in the patch are discarded if the patch program cannot find a place in which to apply them. Like CVS started as a set of scripts on diff3, GNU arch started as a set of scripts on patch. However, fuzzy patch application is a relatively untrustworthy method, sometimes misapplying patches that have too little context (especially ones that create a new file), sometimes refusing to apply deletions that both derivatives have done.Patch commutation
Patch commutation is used inflipdiff
from the "patchutils" package implements patch commutation for traditional patches produced by diff -u.
Weave merge
Weave merge is an algorithm that does not make use of a common ancestor for two files. Instead, it tracks how single ''lines'' are added and deleted in derivative versions of files, and produces the merged file on this information. For each line in the derivative files, weave merge collects the following information: which lines precede it, which follow it, and whether it was deleted at some stage of either derivative's history. If either derivative has had the line deleted at some point, it must not be present in the merged version. For other lines, they must be present in the merged version. The lines are sorted into an order where each line is after all lines that have preceded it at some point in history, and before all lines that have followed it at some point in history. If these constraints do not give a total ordering for all lines, then the lines that do not have an ordering with respect to each other are additions that conflict. Weave merge was apparently used by the commercial revision control tool BitKeeper and can handle some of the problem cases where a three-way merge produces wrong or bad results. It is also one of the merge options of the GNU Bazaar revision control tool, and is used in Codeville.See also
* Comparison of file comparison tools * diff * Branching (revision control)References
{{Version control software Configuration management Version control