Git merge principle

Source: Internet
Author: User
Tags unique id using git

Snapshots


This is the three versions of the project, version 1 has two files A and B, then modifies a, becomes A1, builds a version 2, and then modifies B to B1, forming version 3.

If we save each version of the project to a local repository, we need to save at least 6 files, but in fact, there are only 4 different files, a, A1, B, and B1. To save space, we need to save just one copy of the same file like a method. This introduces the Sha-1 algorithm.

You can use the git command to calculate the Sha-1 value of a file.

echo ' Test content ' | git hash-object--stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4

SHA-1 generates a 40-bit length hash value by calculating the contents of the file.

Sha-1 is very characteristic: the hash value calculated by the contents of the file is the same, the file content is the same

For the content in the above diagram, no matter how many times we execute it, we get the same result. Therefore, the Sha-1 value of the file is the unique ID that can be used as a file. At the same time, it also has an additional function to verify file integrity.

With the help of sha-1, we can make adjustments to the way the project version is stored.



Conflicts

The branching in git is very lightweight, so we use the branch frequently when using Git. It is not unavoidable to merge newly created branches.

Merging branches in Git has two options: Merge and Rebase. But either way, it is possible to have a conflict. So let's take a look at the emergence of conflict.


The situation on the diagram does not solve the problem by moving the branch pointer, it requires a consolidation strategy. First of all, we need to be clear who and who is the merger, is 2,3 and 4,5,6 merge? When it comes to branching, we always associate lines, and we think it's a combination of lines. Actually not, the real merger is 3 and 6. Because each submission contains a complete snapshot of the project, the merge is just a combination of tree and tree.

We can think of a simple algorithm first. Used to compare 3 and 6. But we also need a comparison of the criteria, if only 3 and 6 comparison, then 3 compared with 6, add a file, can be said to be 6 and 3 than the deletion of a file, which does not exactly represent the current state of the conflict. So we select their two bifurcation points (merge base) as reference points for comparison.

Comparisons are made relative to the merge base (commit 1).

First, make a list of all the files in 1, 3, 6, and then iterate through the files in the list. Now let's take an example of a file in the list, which is referred to as version 1, version 3, and version 6 in submissions 1, 3, and 6, respectively. The Sha-1 values for version 1, version 3, and version 6 are identical, which indicates that there is no conflict version 3 or 6 at least one is the same as the version 1 state (meaning that the Sha-1 value is the same or does not exist), and this can be automatically merged. For example, the existence of a file in 1, in 3 did not modify the file, and 6 deleted the file, then 6 can be the version 3 or version 6 are different from version 1, the situation is complex, automatic merge strategy is difficult to take effect, need to manually resolve. Let's take a look at the definition of this state.

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.