今日目標
• 學習什麼是合併衝突
• 模擬多人同時開發的衝突情境
• 掌握手動解決衝突的方法
• 理解團隊協作中衝突產生的原因
重要概念:為什麼會有衝突?
衝突產生的情境:
在團隊開發中,兩個開發者同時從相同的版本開始工作:
初始狀態(上週五下班):
main: A---B
週一同時開工:
小明從 B 建立分支 → 修改同一檔案
小華也從 B 建立分支 → 修改同一檔案的同一行
結果:
C (小明的修改)
/
main: A---B
D (小華的修改)
當小華要合併時 → 衝突!
操作步驟:模擬同時開發
步驟1:設定共同起點
cd company-website
git status
git log --oneline -1
步驟2:模擬開發者A(你)的工作
git checkout -b feature/developer-A
編輯 index.html,修改 h1 標籤:
git checkout -b feature/developer-B
編輯 index.html,修改相同的** h1 標籤:**
git merge feature/developer-A
git log --oneline -2
步驟5:開發者B嘗試合併 → 產生衝突!
git merge feature/developer-B
現在應該看到衝突訊息:
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
步驟6:查看和理解衝突
git status
cat index.html
衝突標記解釋:
<!-- 解決衝突:結合兩個開發者的想法 -->
<h1>歡迎來到我們專業設計的網站</h1>
<p>結合開發者A和B的設計理念,提供最佳使用體驗</p>
<footer>
<p>© 2024 我們公司. 版權所有.</p>
</footer>
git add index.html
git commit
步驟9:清理和檢視結果
git status
git log --oneline --graph -5
git branch -d feature/developer-A
git branch -d feature/developer-B
git push origin main
重要概念總結
衝突產生的核心原因:
git pull origin main
git checkout feature/my-branch
git merge main
git commit -m "完成小功能"
git push origin feature/my-branch
今日學習重點
• ✅ 理解「同時開發」導致衝突的原理
• ✅ 模擬真實團隊協作情境
• ✅ 掌握識別和解決衝突的方法
• ✅ 學習預防衝突的最佳實踐