Git 路線圖Git Roadmap單元 07Unit 07
整合變更Integrate changes

合併與衝突處理Merge branches and resolve conflicts

UNIT 07 · MERGE & CONFLICT

合併與衝突處理Merge branches and resolve conflicts

看懂 Fast-forward、分叉與 conflict,親手完成一個雙親 merge commit。Understand fast-forward merges, diverged branches, and conflicts by creating a two-parent merge commit.

預計時間Estimated time
30–40 分鐘30–40 minutes
單元類型Unit format
教材+模擬器Lesson + simulator
需要先會Prerequisite
理解 branch 與 HEADUnderstand branches and HEAD

conflict 不是 Git 壞掉,而是 Git 不替人做決定

如果 main 沒有新的 commit,merge 可以直接 Fast-forward;如果兩條 branch 都前進,Git 需要整合兩邊的變更,通常會建立雙親 merge commit。

兩邊修改同一段內容時,Git 不知道哪個才是正確答案,因此留下 conflict 標記:

<<<<<<< main
main 的內容
=======
feature 的內容
>>>>>>> feature/login

標準流程是:閱讀兩邊 → 編輯成真正要保留的結果 → git add <path> 標記已解決 → git commit 完成 merge。若判斷不該繼續,可用 git merge --abort 回到開始前。

不要看到 conflict 就只選 ours 或 theirs。真正答案可能是合併兩邊,或重新寫成第三種內容。

A conflict does not mean Git is broken; it means Git will not decide for you

If main has no new commits, a merge can fast-forward. If both branches moved forward, Git must combine their changes and usually creates a two-parent merge commit.

When both sides edit the same lines, Git cannot know the correct answer and leaves conflict markers:

&lt;&lt;&lt;&lt;&lt;&lt;&lt; main
content from main
&#61;&#61;&#61;&#61;&#61;&#61;&#61;
content from feature
&gt;&gt;&gt;&gt;&gt;&gt;&gt; feature/login

The standard flow is: read both sides → edit the result you truly want → run git add <path> to mark it resolved → run git commit to finish the merge. If you should not continue, git merge --abort returns to the starting point.

Do not automatically choose ours or theirs. The right answer may combine both sides or be a third version written from scratch.

INTERACTIVE LAB

在安全模擬器裡親手操作

輸入指令後,Terminal、四個區域與 Git 樹會從同一份虛擬狀態同步更新。

A

WORKING DIRECTORY

正在編輯的檔案

  • README.md已提交
  • app.js已提交
  • nav.js已提交
B

STAGING AREA

下一版準備內容

  • Staging Area 是空的
C

LOCAL REPOSITORY

電腦裡的版本

HEAD
→ main
目前 commit
c3
commit 數
4
D

REMOTE REPOSITORY

GitHub 示意狀態

origin
尚未設定
遠端 branch
沒有 commit
網路連線
僅記憶體模擬

教學 Terminal

不會執行真實 Git 或 shell

教學模擬器已就緒。這裡不會執行真實 Git 或讀寫你的檔案。

點一下填入:

VIRTUAL FILES

虛擬檔案編輯器

更多情境工具

LIVE GIT TREE

commit 與 branch

HEAD → main
  1. c4
    feature/login
    完成登入提示parent:c2
  2. c3
    HEAD → main
    調整首頁標題parent:c2
  3. c2
    加入導覽列parent:c1
  4. c1
    建立專案parent:無(第一個 commit)

CHECKPOINT

離開前,確認你能用自己的話說明Before you leave, explain it in your own words

  1. 這些指令改變 Git 的哪一個區域?
  2. Git 樹上的 branch 與 HEAD 移到哪裡?
  3. 失敗時 repository 是否保持原狀?
  4. 真實專案操作前,應先查看什麼?
  1. Which Git area does this command change?
  2. Where do the branch and HEAD move in the Git tree?
  3. Does the repository stay unchanged when the command fails?
  4. What should you check before operating on a real project?

免責聲明:本站內容為個人學習、實作與經驗分享紀錄,部分內容透過 AI 協助修正與整理。由於每位使用者的系統環境、軟體版本、權限設定與操作方式不同,實際結果可能有所差異。請在操作前自行評估風險並備份重要資料。因參考本站內容所造成之任何系統異常、資料遺失、設定錯誤、設備故障、服務中斷或其他直接、間接損失,均由使用者自行承擔,本站與作者不負任何相關責任。

Disclaimer: This site records personal learning, implementation, and experience sharing, with some content edited and organised with AI assistance. Results may differ because every user has a different system environment, software version, permission setup, and workflow. Assess risks and back up important data before trying anything. You are responsible for any system errors, data loss, configuration mistakes, equipment faults, service interruptions, or other direct or indirect losses resulting from using this content.