How does git resolve conflicts?

How does git resolve conflicts?

1. Under what circumstances will conflict arise?

When multiple developers use or operate the same file in git at the same time:

  • Multiple branch codes are merged into one branch;
  • Multiple branches push code to the same remote branch;

The essence of the push operation is to merge the local code into the remote branch; similarly, the essence of the pull operation is to merge the remote branch into the local branch. Therefore, there may be conflicts when performing these two operations.

Sometimes resolving conflicts does not mean everything is ok, because there will still be logical conflicts in some cases.

For example: User A modifies the return value of the function, and user B still uses the old return value. Although the merge conflict is successful, there is a logical conflict.

insert image description here

2. Resolution of conflicts

After a conflict occurs, the contents of the conflicting file in the file system will become similar to this:

a123
<<<<<<< HEAD
b789
=======
b45678910
>>>>>>> 6853e5ff961e684d3a6c02d4d06183b5ff330dcc
c
1234567

Among them: the content between the conflict mark <<<<<<< and ======== is my modification ======== and the content between >>>>>>> is someone else's modification .

Modifications can be retained depending on the situation:

Keep your own:
remove ======= and >>>>>>>, then remove <<<<<<<

Keep others':
delete <<<<<<< and =======, then delete >>>>>>>

All reserved:
delete <<<<<<<, =======, >>>>>>>

After modification, execute:

git add 冲突文件名
git commit -m "xxx"
git push  //直接push就可以了,因为刚刚已经执行过相关merge操作了
123

1. Precondition: No files can be modified on the master branch. Changes to the master branch are only available via git pull and git merge.
2. We have a branch to modify the code, for example, my branch is called dev branch. I have modified the code, and now I don't know if there is a conflict.
3. In the dev branch, execute the command git merge origin/master to merge the remote master branch into the current dev branch.
4. If there is no error, go directly to step 5. If there is a conflict, according to the prompt, resolve the conflict and save the file. Then execute the command git add xxx to add the file you modified to the cache. Then execute the command git commit -m "xxx" to add commit information.
5. Execute the following command to switch to the master branch: git checkout master.
6. Execute the command git pull to ensure that the current master branch is the latest code.
7. Merge the code of the dev branch back into the master branch: git merge dev.
8. Submit the code: git push.

3. Cancel the merger

If conflict handling is not in place, or you don’t want to merge and other factors want to undo the merge, you can enter the 'git reset --hard' command in the console, and the merge operation will be safely revoked and return to the state before you started the merge .

Guess you like

Origin blog.csdn.net/Wis57/article/details/129754370