合併衝突

Git 中級

George Boorman

Curriculum Manager, DataCamp

衝突

  • 衝突

    • 在分支之間,無法協調一個或多個檔案內容的差異
  • 在兩個分支都編輯同一個檔案

  • 嘗試合併

  • Git 不知道要保留哪個版本

  • 發生衝突!

Git 中級

README.md 的衝突版本

documentation branch

# Contents and usage

This repo contains source code 
for the DataCamp website.

It also contains source code for an 
AI-Assistant (recommendation system) 
that takes prompts from learners and 
returns suggested content
that they might be interested in. 

It is for internal use only, 
external access is prohibited.

main branch

# Contents and usage

This repo contains source code 
for the DataCamp website.

It is for internal use only, 
external access is prohibited.
Git 中級

合併

  • main 分支執行
git merge documentation
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
Git 中級

開啟檔案

nano README.md

使用 nano 檢視含有衝突區塊的 README 檔案

Git 中級

Git 衝突語法

標註的衝突檔案,顯示 main 分支與 documentation 分支的內容差異

Git 中級

解決衝突

在 nano 網頁編輯器中刪除 Git 語法 <<<<<<< documentation、=======、以及 >>>>>>> HEAD

  • 儲存:按 Ctrl + O(不是 Ctrl + 0),再按 Enter
  • 離開:按 Ctrl + X
Git 中級

合併分支

  • 衝突解決後再進行合併
git add README.md
git commit -m "Resolving README.md conflict"
git merge documentation
Already up to date.
  • 預防勝於治療!
Git 中級

一起來練習吧!

Git 中級

Preparing Video For Download...