Conflict generation and resolution under Git during multi-person development

conflict arises

Many commands can conflict, but fundamentally, it is merge and patch (applying patches) conflicts.
And rebase is the process of re-setting the benchmark and then applying the patch, so there will be conflicts.
git pull will automatically merge, and repo sync will automatically rebase, so git pull and repo sync will also conflict. Of course, not to mention git rebase.

type of conflict

logical conflict

git's automatic processing (merging/applying patches) succeeds, but the logic is flawed.
For example, another person has changed the file name, but I still use the old file name. In this case, the automatic processing can be successful, but there is actually a problem.
For another example, the meaning of the return value of the function changes, but I still use the old meaning. This situation is automatically handled successfully, but there may be major bugs hidden. This kind of problem is mainly guaranteed by automated testing. Therefore, it is better to be able to write a relatively complete automated test case.
The solution to this conflict is to make a BUG correction. Not really resolve conflicts reported by git.

Content conflict

Two users modify the same area of ​​the same file, and git will report a content conflict. This is what we often see, and the solutions that follow are mainly aimed at this kind of conflict.

tree conflict

Conflicts caused by file name modification are called tree conflicts.
For example, if user a renames the file to ac, and user b renames the same file to bc, then when b merges the two commits, a conflict will occur.
$ git status
    added by us :     b.c
    both deleted :    origin - name.c
    added by them :   a.c
If you finally decide to use bc, then the solution is as follows:
git  rm  a.c
git  rm  origin - name.c
git add b.c
git commit
When the first two git rms are executed, "file-name : needs merge" will be warned, and you can ignore them.
 
Tree conflicts can also be resolved with git mergetool, but the entire resolution process is done in an interactive Q&A, using d to delete unnecessary files and c to keep needed files.
Finally, execute git commit to submit.

Content conflict resolution

find conflict

Generally, the word "CONFLICT" is used when there is a conflict:
$ git pull
Auto - merging  test .txt
CONFLICT (content) :  Merge conflict  in  test .txt
Automatic merge failed; fix conflicts  and  then  commit the result.
 
However, there are exceptions. The error reported by repo sync may not directly indicate a conflict, but the following:
error : project mini /sample
注:无论是否存在冲突,只要本地修改不是基于服务器最新的,它都可能报告这个错误,解决方法都是一样。
 
这个时候,需要进入报错的项目(git库)目录,然后执行git rebase解决:
git rebase remote -branch -name

冲突解决的一般过程

merge/patch的冲突解决

先编辑冲突,然后git commit提交。
注:对于git来讲,编辑冲突跟平时的修改代码没什么差异。修改完成后,都是要把修改添加到缓存,然后commit。

rebase的冲突解决

rebase的冲突解决过程,就是解决每个应用补丁冲突的过程。
解决完一个补丁应用的冲突后,执行下面命令标记冲突已解决(也就是把修改内容加入缓存):
git add  -u
注:-u 表示把所有已track的文件的新的修改加入缓存,但不加入新的文件。
 
然后执行下面命令继续rebase:
git rebase  -- continue
有冲突继续解决,重复这这些步骤,直到rebase完成。
 
如果中间遇到某个补丁不需要应用,可以用下面命令忽略:
git rebase  --skip
 
如果想回到rebase执行之前的状态,可以执行:
git rebase  --abort
 
注:rebase之后,不需要执行commit,也不存在新的修改需要提交,都是git自动完成。

编辑冲突的方法

直接编辑冲突文件

冲突产生后,文件系统中冲突了的文件(这里是test.txt)里面的内容会显示为类似下面这样:
a123
<< << << < HEAD
b789
== == == =
b45678910
>> >> >> >  6853e5ff961e684d3a6c02d4d06183b5ff330dcc
c
其中:冲突标记<<<<<<< (7个<)与=======之间的内容是我的修改,=======与>>>>>>>之间的内容是别人的修改。
此时,还没有任何其它垃圾文件产生。
 
最简单的编辑冲突的办法,就是直接编辑冲突了的文件(test.txt),把冲突标记删掉,把冲突解决正确。

利用图形界面工具解决冲突

如果要解决的冲突很多,且比较复杂,图形界面的冲突解决工具就显得很重要了。
 
执行git mergetool用预先配置的Beyond Compare解决冲突:
$ git mergetool
Merging :
test.txt

Normal merge conflict  for  'test.txt' :
  {local} : modified
  {remote} : modified
 
 
上面三个窗口依次是“LOCAL”、“BASE”、“REMOTE”,它们只是提供解决冲突需要的信息,是无法编辑的。
下面一个窗口是合并后的结果,可以手动修改,也可以点击相应颜色的箭头选择“LOCAL”或者“REMOTE”。
 
在Beyond Compare中修改冲突保存后,冲突文件(test.txt)中的冲突标记就没有了,成了修改后的内容,一个文件的冲突编辑就完成了。
 
注意:
启动Beyond Compare之后,会自动生成几个包含大写字母名称、数字的辅助文件:
 
关闭Beyond Compare时,这几个辅助文件都会自动删除,但同时会生成一个test.txt.orig的文件,内容是解决冲突前的冲突现场。
默认该.orig文件可能不会自动删除,需要手动删掉。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518128&siteId=291194637