16.4.2 图形工具完成冲突解决

上面介绍的通过手工编辑完成冲突解决并不复杂,对于简单的冲突是最快捷的解决方法。但是如果冲突的区域过多、过大,并且缺乏原始版本作为参照,冲突解决过程就会显得非常的不便,这种情况下使用图形工具就显得非常有优势。

还以上面的合并冲突为例介绍使用图形工具进行冲突解决的方法。为了制造一个冲突,首先把user2辛辛苦苦完成的冲突解决提交回滚,再执行合并重新进入冲突状态。

(1)将冲突解决的提交丢弃,即强制重置到前一个版本。


$git reset—hard HEAD^


(2)这时查看状态,会显示当前工作分支的最新提交和共享版本库的master分支的最新提交出现了偏离。


$git status

On branch master

Your branch and 'refs/remotes/origin/master' have diverged,

and have 1 and 1 different commit(s)each,respectively.

#

nothing to commit(working directory clean)


(3)那么执行合并操作吧。冲突发生了。


$git merge refs/remotes/origin/master

Auto-merging doc/README.txt

CONFLICT(content):Merge conflict in doc/README.txt

Automatic merge failed;fix conflicts and then commit the result.


下面就演示使用图形工具如何解决冲突。使用图形工具进行冲突解决需要事先在操作系统中安装相关的工具软件,如:kdiff3、meld、tortoisemerge、araxis等。而启动图形工具进行冲突解决也非常简单,只须执行命令git mergetool即可。


$git mergetool

merge tool candidates:opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff

diffuse ecmerge p4merge araxis emerge vimdiff

Merging:

doc/README.txt

Normal merge conflict for 'doc/README.txt':

{local}:modified

{remote}:modified

Hit return to start merge resolution tool(kdiff3):


运行git mergetool命令后,会显示支持的图形工具列表,并提示用户选择可用的冲突解决工具。默认会选择系统中已经安装的工具软件,如kdiff3。直接按下回车键,自动打开kdiff3进入冲突解决界面。

启动kdiff3后,如图16-5所示,上方三个窗口由左至右显示冲突文件的三个版本,分别是:

A:暂存区1中的版本(共同祖先版本)。

B:暂存区2中的版本(当前分支更改的版本)。

C:暂存区3中的版本(他人更改的版本)。

kdiff3下方的窗口是合并后文件的编辑窗口。如图16-6所示,点击标记为“合并冲突”的一行,在弹出菜单中出现A、B、C三个选项,分别代表从A、B、C三个窗口复制相关内容到当前位置。

16.4.2 图形工具完成冲突解决 - 图1

16.4.2 图形工具完成冲突解决 - 图2

16.4.2 图形工具完成冲突解决 - 图3

16.4.2 图形工具完成冲突解决 - 图4


doc/README.txt.orig


查看暂存区会发现暂存区中的冲突文件的三个副本都已经清除。


$git ls-files-s

100644 463dd451d94832f196096bbc0c9cf9f2d0f82527 0 doc/README.txt

100644 430bd4314705257a53241bc1d2cb2cc30f06f5ea 0 team/user1.txt

100644 a72ca0b4f2b9661d12d2a0c1456649fc074a38e3 0 team/user2.txt


执行提交和推送。


$git commit-m "Say hello to all users."

[master 7f7bb5e]Say hello to all users.

$git push

Counting objects:14,done.

Delta compression using up to 2 threads.

Compressing objects:100%(6/6),done.

Writing objects:100%(8/8),712 bytes,done.

Total 8(delta 0),reused 0(delta 0)

Unpacking objects:100%(8/8),done.

To file:///path/to/repos/shared.git

a123390..7f7bb5e master->master


查看最近三次的提交日志,会看到最新的提交是一个合并提交。


$git log—oneline—graph-3

*7f7bb5e Say hello to all users.

|\

|*a123390 Say hello to user1.

*|60b10f3 Say hello to user2.

|/