How does conflict arise

Git, as we all know, is essentially a tree. For example, if you have a node tree (node0),

Based on node0, we developed node node1. Based on node0, we developed node2. If we operate on the same location in the same file on node1 and node2, we will definitely cause a conflict.

File: test. TXT

HelloWorld!

A has updated version 2:

HelloJavaScript!

B updated version 3:

HelloVue!

The scenario is as follows: Both a and B are developed based on the test. TXT file. A develops version 2 and submits the code; B has developed version 3 and also needs to submit the code. In this case, an error conflict will be reported.

Why is that? After a develops and submits the version, the remote code is already version 2 code, while B develops version 3 based on version 1. Therefore, if b wants to submit the code, it must update its own code to version 2 code and then submit it. If there is a conflict, it will resolve the conflict and then submit it.

Ii. How to resolve conflicts

1. Create a fix-conflict folder and add a test.txt file to this folder.

And add something to this file:

Git add. Git commit -m ‘XXX’

Git status:

OK, there is nothing left to submit, the working tree is now clean.

2. Create a branch

To create a branch, execute git checkout -b develop to create a branch called develop and check it out.

Then modify the contents of the test.txt file:

Git status:

Obviously, our last change hasn’t been put in staging yet. Then run the following command:

OK, the branch’s work tree is also clean.

3. Switch to the main branch, modify the content, and merge the content

Use Git Checkout Master to switch to the main branch

Modify the contents of test.txt:

Execute the following code:

Then merge the previous Develop branch:

Perfect. There’s a conflict. We can open SourceTree and import the corresponding file to see the conflict (or use a common code editor such as VSCode).

In the figure above, the HEAD on the right represents the version indicated (that is, where your master branch is, because you already checked it out when you ran the merge command) in the upper part of the section (the upper part of =======), The version indicated by the Develop branch is in the lower part of =======. To resolve the conflict, you must choose to use one of the two sections divided by =======, or you can combine the contents yourself.

4. Conflict resolution

Right-click the conflicting file, then select Conflict Resolution and click Use my version or Use others version to resolve the conflict (select my version to use the content of the current branch, others version to use the content to merge).

Then enter what you did in the white box below and click Submit:

Ok, so that’s the question about how conflicts arise and how to resolve them.