Git現在越來越火,很多人都從Svn遷移到Git上面,Git讓我們更加與世界接軌,不再是“局域網”的程序猿,特別是掌握了Git之后,會發現它真的很好用,本文對Git中比較煩人的沖突進行了詳細的說明,希望能幫助那些剛接觸Git的程序猿。
亮點:
- 采用可視化SourceTree插件beyondCompare更加接近svn沖突比較
構造沖突
- A 修改了conflict.file 中第1行內容並且提交到git上
- B 這個時候也修改了confilct.file中第一行內容准備提交,這個時候git就會提示
To git@192.168.x.xxx:xxx/server-aggregator.git ! [rejected] develop -> develop (fetch first) error: failed to push some refs to 'git@192.168.xx.xx:xxx/server-aggregator.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
提示遠程已經有更新了,本地版本太低,讓我們先pull拉取最新的代碼,
- 我們繼續拉取代碼pull一下,這個時候由於本地有修改這個文件,就會在本地產生沖突文件
配置外部比較工具
- 下載地址:http://www.scootersoftware.com/download.php
- 打開sourcetree->工具->選項->比較->外部差異對比合並->選擇BeyondCompare
解決沖突
-
在本地副本->右鍵->解決沖突->打開外部合並工具
-
和svn一樣解決好沖突保存更改,退出即可
另外一種情況
- 拉取時出現如下提示:
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
提示需要暫存本地修改,才能拉取服務器上新的代碼
-
點擊貯存(英文版:Stash),隨便起一個名字,里面存的都是距離上次服務器版本到本地修改之間的差異,千萬別刪掉了,合並成功無誤了再刪掉。
-
pull拉取服務器代碼,這個時候,本地的代碼變成了服務器上的代碼
- 點擊貯藏->應用貯藏區 ,這個時候是把之前的修改合並到本地上,這個時候會提示沖突
git -c diff.mnemonicprefix=false -c core.quotepath=false stash apply stash@{0} Auto-merging server/conflict.file CONFLICT (content): Merge conflict in server/conflict.file
可以在sourcetree里看到有感嘆號,代表沖突文件,和上面解決沖突方法類似,但是稍微不同,最左邊成了遠程版本,中間為遠程上一個版本,最后才是本地修改。
這個是和我們操作方式有關:我們是先暫存本地修改,先拉取遠程代碼,這個時候local 就成了遠程代碼,最后我們用暫存的合並進去,remote就成了本地修改
生成了多余的.orig文件
這個是由於git自身造成的 它會解決沖突后 生成一個原來沖突的備份,我們可以去掉
git config --global mergetool.keepBackup false
感謝您的耐心閱讀,如果您發現文章中有一些沒表述清楚的,或者是不對的地方,可以給我留言
,當然你有其他類的需求文章,也可以私信我。