Home  >  Article  >  Development Tools  >  Why does git produce conflicts?

Why does git produce conflicts?

WBOY
WBOYOriginal
2022-06-24 10:38:1710858browse

In git, the reason for conflicts is that when merging files, the same location of the same file has been modified, and the content is different; that is, the same location of the same file in two submitted branches is different. The operations were merged, so conflicts occurred; common operations that cause conflicts include merging two branches and pulling remote warehouse code to local.

Why does git produce conflicts?

The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.

Why does git conflict

1. Common conflict operations:

  • Merge two branches

  • Pull the code from the remote warehouse to the local

2. Cause of conflict:

When merging files, if the same location of the same file is modified and the content is different, a conflict will occur

Three. Solution steps:

1. git status View conflict files

2.vim src/main/java/a.txt (assuming a.txt is a conflict file, here it needs to be a file full path), manually modify the conflict part

3.git add src/main/java/a.txt Tell Git that the conflict is resolved

4.git commit -m 'Resolve conflict' ( Resolve conflicts as comments, customizable)

Different operations of the same files in the same location of two submitted branches are merged.

Practical demonstration

(1) Scenario

Two different branches in the local library modify the same file and the same code block. The two branches merge the modifications into the master branch, and the master merges the second one. When branching the code, an error is reported: merge conflict.

(2) Local library

<1>master branch

##<2> ;Create two branches

<3>Modify and submit two branches

aBranch branch:

bBranch branch:

(3) Conflicts occur when merging branches

Merge aBranch branch (merge aBranch branch into the current master branch):

Note:

git merge: By default, Git performs a "fast-farward merge" and will directly point the Master branch to Develop branch.
After using the --no-ff parameter, normal merge will be performed and a new node will be generated on the Master branch. In order to ensure the clarity of version evolution, it is recommended to adopt this method.

Merge bBranch branch again, resulting in conflict:

##mergeTest.txt file content:

(4) Resolve conflicts

On the current branch (master), find the conflict file, directly modify the conflict code, add, and commit.

Note: Simple method, use vim to modify and cat to view conflict files. (Be careful to delete the conflict code delimiter automatically generated by git)

(5) Complete the conflict resolution

Note: Submitting or merging will generate git nodes. Each node corresponds to a code version.

Recommended learning: "Git Tutorial"

The above is the detailed content of Why does git produce conflicts?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn