- Git 入門
- Git - 首頁
- Git - 版本控制
- Git - 基本概念
- Git - 命令列
- Git - 安裝
- Git - 首次設定
- Git - 基本命令
- Git - 獲取幫助
- Git - 工具
- Git - 速查表
- Git - 術語
- Git 分支
- Git - 簡述分支
- Git - 建立新分支
- Git - 切換分支
- Git - 分支和合並
- Git - 合併衝突
- Git - 管理分支
- Git - 分支工作流程
- Git - 遠端分支
- Git - 跟蹤分支
- Git - 變基
- Git - 變基與合併
- Git - 合併提交
- Git 操作
- Git - 克隆操作
- Git - 打標籤操作
- Git - 別名操作
- Git - 提交操作
- Git - 暫存操作
- Git - 移動操作
- Git - 重新命名操作
- Git - 推送操作
- Git - 拉取操作
- Git - Fork 操作
- Git - 修補程式操作
- Git - 差異操作
- Git - 狀態操作
- Git - 日誌操作
- Git - HEAD 操作
- Git - origin master
- Git 撤銷
- Git - 撤銷更改
- Git - 檢出
- Git - 恢復
- Git - 重置
- Git - 恢復操作
- Git - Rm
- Git - 切換操作
- Git - Cherry-pick
- Git - 修正
- Git 在伺服器上
- Git - 本地協議
- Git - 智慧 HTTP 協議
- Git - 啞 HTTP 協議
- Git - SSH 協議
- Git - Git 協議
- Git - 在伺服器上獲取 Git
- Git - 設定伺服器
- Git - 守護程序
- Git - GitWeb
- Git - GitLab
- Git - 第三方託管選項
- 分散式 Git
- Git - 分散式工作流程
- Git - 為專案貢獻程式碼
- Git - 維護專案
- 自定義 Git
- Git - 配置
- Git - 鉤子
- Git - 屬性
- Git - Init
- Git - Commit
Git - 合併衝突
在 Git 中合併分支
合併是將一個分支的更改整合到另一個分支的過程。在單獨的分支上開發功能或修復錯誤後,通常會將該分支合併回主分支,以將這些更改包含在主程式碼庫中。
處理合併衝突
分支合併並不總是按計劃進行。如果 **auth-module** 分支和 **bugfix** 分支都對同一檔案的同一部分進行了不同的編輯,則 Git 會遇到合併衝突。
這就是它的表現方式
$ git merge auth-module Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.
Git 報告 `index.html` 檔案中的衝突並停止合併過程。
它等待我們手動解決衝突,透過編輯檔案而不是自動建立新的合併提交。
我們可以使用 **git status** 檢視哪些檔案存在衝突
$ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add..." to mark resolution) both modified: index.html no changes added to commit (use "git add" and/or "git commit -a")
當 Git 在合併期間發現衝突的更改時,它會將受影響的檔案標記為未合併,並在檔案中新增衝突標記以識別衝突的部分。
`index.html` 中的衝突部分可能如下所示
<<<<<<< HEAD:index.html <header> <h1>Welcome to our Website</h1> </header> ======= <header> <h1>Welcome to the Best Website</h1> </header> >>>>>>> feature/update-header:index.html
在上面的示例中
衝突表明 `index.html` 標題部分中的 <h1> 標籤已被 feature/update-header 分支和 master 分支 (**HEAD**) 不一致地修改。
我們必須手動編輯檔案以決定保留哪些更改以及如何合併它們,以解決此衝突。
例如,我們可以選擇透過修改 `index.html` 以包含這兩個概念來合併修改。
<header> <h1>Welcome to our Website</h1> </header>
在解決 `index.html` 中的衝突後,我們應該從檔案中刪除衝突標記 (**\\\<<<\, =======, 和 >>>>>>**)。
接下來,使用 **git add** 暫存檔案並將其標記為已解決在 Git 中。
$ git add index.html
在所有衝突檔案都已暫存後,接下來提交合並。
$ git commit
如果我們更願意使用圖形工具,可以使用 **git mergetool** 啟動一個可視化合並工具,以幫助解決衝突。
$ git mergetool
This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare vimdiff emerge
Merging:
index.html
Normal merge conflict for 'index.html':
{local}: modified file
{remote}: modified file
Hit return to start merge resolution tool (opendiff):
一旦我們使用指定的工具解決衝突並關閉視窗,Git 會要求我們驗證合併是否成功。
如果收到確認,Git 會自動暫存已解決的檔案並將其標記為已解決。
透過再次執行 git status,我們可以確認衝突的狀態。
$ git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: index.html
一旦確保所有衝突都已處理並暫存,使用 **git commit** 將完成合並提交。
通常,預設提交訊息類似於以下內容
Merge branch 'feature/auth-module'
Conflicts:
index.html
# It seems like you're committing a merge.
# If this isn't correct, please remove the file
# .git/MERGE_HEAD and try again.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
# modified: index.html