How to Implement and work with the merge command in Git

Source: Internet
Author: User

How to Implement and work with the merge command in Git
Imagine the following situation: there are two branches in the code library, and each branch has been modified. Finally, you want to merge one of them into another branch. Personal blog web site http://swinghu.github.com/
What is the process of merging? Does Git operate on each branch according to its historical data in serialization, or does it only merge the last version of the file in each branch? This is a problem. I want to analyze the merge operations of git.
Recall that the internal structure of the Git version library is organized to a directed acyclic graph (directed acyclic graph): Each commit generates a snapshot of the version tree (snapshot ), in addition, this snapshot saves a reference pointing to its parent node (the last snapshot submitted for this branch) (usually only one parent node is currently submitted, but no parent node exists for the snapshot submitted in the initial test, A merge operation has two or more parent nodes ). In this way, each commit recursively creates references for certain node sets pointing to the parent node. Sometimes, when we consider comparing the differences between the parent node submission tree and the current node submission tree (diff), we think of one commit as one patch) it helps us understand the working mechanism of git. In this way, we can think that the commit tree is integrated with patches for all parent nodes. A tree that performs merge operations on two branches. Therefore, it can be considered that the two branches have applied all their parent node patches, and then perform a Union operation.
But that is not the real execution method of git merge, because, first of all, if you work that way, the execution will be very slow !, In addition, during the execution process, it will re-process all the conflicts caused by the previous merge. So, how does git merge actually operate?
I like to think in mathematics: Two submissions are given. A And B , Merge and submit (commit) Operations A branch B It can be described as follows: [A between B] = [A] + [B]? [C] Here C Yes A And B The merging items (the parts shared by the ancestor of the recently submitted tree) must be "subtracted" C Because if this is not the case, we will have two A branch B . This operation X + y? Z It is called a three-way merge. You can consider the execution path X? Z Apply X Or X? Z Apply Y . In fact, the diff and patch operations do not follow the above operations. Instead, they are implemented using the longest common subsequence algorithm. X? W And Sequence X The difference between series w is the value assigned when we know the longest common subsequence (the public part of the two sequences may be removed in the middle ). To construct a three-way merge X + y? W , We X And W Assign a value to the public subsequence. Y And W Assign values when finding common subsequences, and then output each of them either: the common part of the three sequences, or
In X But in Y And W Does not exist in, or Y But in X And W Does not appear in Y And W However X Does not appear in, or appears in X And W But in Y . X: w: y :? Merged: milk milkjuice juiceflour flour floursausage sausagegiteggs eggs eggsbutter butter ~ /. Gitconfiggit config -- global merge. conflictstyle diff3I had two eggs and three sausages for breakfast. X And Y But in W Does not appear in W But not in X And Y Appears

  1. Git merge feature
  2. Git merge-base master feature
  3. Git commit-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.