采用可視化SourceTree插件beyondCompare解決沖突
1、構造沖突
(1)修改了server.xml文件的第40行內容並且提交推送到遠程庫上;
(2)另外一個工作目錄下也修改了該文件的低40行內容,並且也要推送到遠程庫上去;
推送的時候出現如下問題:
git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin master:master Pushing to git@xxxxx.com:xxxxx/test.git To git@xxxxxxxx.com:xxxxxx/test.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@xxxxxxx.com:xxxxxx/test.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 完成時帶有錯誤,見上文。
提示遠程已經有更新了,本地版本太低,讓我們先pull拉取最新的代碼然后再次推送。
(3)接着pull代碼到本地就會出現沖突,如下所示:

2、配置外部比較工具
(1)打開sourcetree ----> 工具 ----> 選項。

(2)按照下圖所示的配置:

(3)在用戶目錄中找到 .gitconfig 文件並打開,例如,C:\Users\%用戶名%\.gitconfig。
增加如下的信息並保存。
[user] name = 配置的git賬號 email = 配置git時的郵箱 [core] autocrlf = true [difftool "sourcetree"] cmd = 'd:/Program Files (x86)/Beyond Compare 3/BComp.exe' \"$LOCAL\" \"$REMOTE\" [mergetool "sourcetree"] cmd = 'd:/Program Files (x86)/Beyond Compare 3/BComp.exe' \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\" trustExitCode = true [diff] tool = sourcetree [difftool] prompt = false [merge] tool = sourcetree [mergetool] prompt = false
3、解決沖突
(1)在本地副本的已暫存文件 ----> 右鍵 ----> 解決沖突 ----> 打開外部合並工具。

啟動Beynod Compare軟件需要一會時間,接着可以看到如下所示的信息:
(2)接着關閉Beynod Compare工具,沖突的那個感嘆號沒有了,並且會有一個 .orig 的文件生成。接着選中那個.orig文件,單擊右鍵 ----> 移除。
接着 commit、push。

附加:另外一種可能出現沖突的情況
拉取時出現如下所示:
it -c diff.mnemonicprefix=false -c core.quotepath=false pull local-server-aggregator develop /opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-shell:3: warning: Insecure world writable dir /usr in PATH, mode 040777 From 192.168.0.200:weitoo/server-aggregator * branch develop -> FETCH_HEAD Updating b0c5c94..40cef3b error: Your local changes to the following files would be overwritten by merge: server/conflict.file Please, commit your changes or stash them before you can merge. Aborting
上面信息提示需要暫存本地修改,才能拉取服務器上新的代碼。
(1)接着進行暫存的處理操作:
-
點擊貯存(Stash),隨便起一個名字,里面存的都是距離上次服務器版本到本地修改之間的差異,千萬別刪掉了,合並成功無誤了再刪掉。
-
pull拉取服務器代碼,這個時候,本地的代碼變成了服務器上的代碼
- 點擊貯藏->應用貯藏區 ,這個時候是把之前的修改合並到本地上,這個時候會提示沖突,如下所示:
git -c diff.mnemonicprefix=false -c core.quotepath=false stash apply stash@{0} Auto-merging server/server.xml CONFLICT (content): Merge conflict in server/server.xml
(2)可以在sourcetree里看到有感嘆號,代表沖突文件,和上面解決沖突方法類似,但是稍微不同,最左邊成了遠程版本,中間為遠程上一個版本,最后才是本地修改。
這個是和我們操作方式有關:我們是先暫存本地修改,先拉取遠程代碼,這個時候local 就成了遠程代碼,最后我們用暫存的合並進去,remote就成了本地修改。

(3)與上面處理沖突的方法一樣。
