SourceTree + Beyond Compare解決Git衝突的方法
採用視覺化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 [email protected]:xxxxx/test.git To [email protected]
:xxxxxx/test.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '[email protected]: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 [email protected]{0}
Auto-merging server/server.xml
CONFLICT (content): Merge conflict in server/server.xml
(2)可以在sourcetree裡看到有感嘆號,代表衝突檔案,和上面解決衝突方法類似,但是稍微不同,最左邊成了遠端版本,中間為遠端上一個版本,最後才是本地修改。
這個是和我們操作方式有關:我們是先暫存本地修改,先拉取遠端程式碼,這個時候local 就成了遠端程式碼,最後我們用暫存的合併進去,remote就成了本地修改。
(3)與上面處理衝突的方法一樣。