The root cause of conflicts in Git merge, how to solve various types of conflicts

foreword

Briefly introduce the conflicts caused by deviating branches, merge branches, and conflicts caused by fetch, merge, pull, and push. The reason for the conflict and how to resolve it manually.

The reason for the conflict (Merge)

The same line of code in the same file has two commits to modify it. If it is merged (merge), a conflict will occur.

Introduced by a common bug when pulling code

The pull code shows the error as shown in the figure:
insert image description here

The solution to this problem is very simple, execute the following code in turn, and then resolve the conflict:

git config pull.ff false   
git pull

But the analysis of the reasons is intriguing.

Prerequisite knowledge:

  • git pull = git fetch + git merge

  • git fetch pulls the latest content of the remote host to the local, and the user decides whether to merge into the working local branch after checking. If you decide to merge into your local branch, you can use git merge to merge into your working local branch!

  • The git-merge command is an operation used to merge from the specified commit(s) into the current branch.

  • There are two cases of git merge , one is the usage of git merge and cherry-pick (for merging from one branch to another).
    The other is used in git-pull to integrate changes in another code repository (ie: git pull = git fetch + git merge)

The reason for the problem (very important)

With the above prerequisite knowledge, you can know the reason why git pull fails: it is not because of the failure of pull, but because of the failure of merge. When executing the pull, git merge tries to merge the commits of the remote host (three commits in blue in the figure below) to the local branch. But your local branch has git add and git commit a commit. So the local branch and the remote branch are merged. Normally, this kind of merge will not be a problem, but if the following situation occurs, there will be a conflict. Let's continue to see the reasons for the conflict:

The reason for the conflict (Merge)

The same line of code in the same file has two commits to modify it. If it is merged (merge), a conflict will occur.

The two cases of merge mentioned in the green font above (including merge and cherry-pick) may conflict!

With the above knowledge, with the following figure, you can understand the reason for the error:

insert image description here

Reflections on Two Conflict Handling Methods

Solution 1: Prioritize git merge (git pull includes merge), then git add and git commit. This is only possible if there are fewer developers (just yourself). Because doing so risks losing code!

If you have written code locally without git add and git commit, someone else has modified the code in the same location remotely, and you pull it down (execute merge), it is possible to flush out your code, or report a conflict directly.

This method is generally not advisable. Look directly at method two.

Solution 2: After writing the code, add and commit first, and then perform the merge operation (git pull includes merge), and then there will be conflicts. It's not terrible to have a conflict, it's just a solution. See below how to resolve conflicts manually:

git resolve conflicts manually:

The content between <<<<<<<HEAD and ======= is the content in code block A The content
between ======= and >>>>>>> is The content in code block B is shown in the
following figure:
insert image description here
Solution: Manually delete the content of code block A, or manually delete the content of code block B; or merge the code between A and B

Then delete the redundant >>>>> symbols and ====== symbols

Finally, temporarily save and submit.

Guess you like

Origin blog.csdn.net/weixin_44757863/article/details/121618060