> For the complete documentation index, see [llms.txt](https://docs.webmix.cc/git/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.webmix.cc/git/4-git-ji-ben-cao-zuo/4.14-he-bing-yu-dao-chong-tu.md).

# 4.14 合併遇到衝突

## 狀況

當分支在做合併的時候，可能會遇到相同檔案有改到相同地方，那就會造成合併上的衝突。

Git 會在遇到衝突的地方，加入一些「標記行」，需自行手動解決衝突，解決後，再執行 **`git add`** 及 **`git commit`** 來提交合併。

發生衝突後，若要放棄合併，可執行以下：

```bash
git merge --abort
```

發生衝突後，也解決了衝突的狀況，就執行以下指令( `add` 和 `commit` )進行合併：

```bash
git add c.txt
git commit -m "conflict fixed"
```

## 示意圖

同樣的檔案(c.txt)，在不同的 branch，改到了同一行，例：

<figure><img src="/files/zq3HJ0To8SXvI7iiYyQI" alt=""><figcaption></figcaption></figure>

將 feature1 合併到 main：

<figure><img src="/files/WAC74zsW5Qd3ncBNkYa4" alt=""><figcaption></figcaption></figure>

發現產生了衝突，Sourcetree 上的示意：

<figure><img src="/files/OHnobR7wGpB07SBLz10e" alt=""><figcaption></figcaption></figure>

在 VS Code 上，瀏覽衝突所產生的標記行，可直接修改成想要的文字：

<figure><img src="/files/sNJRTp5qIKMXgx6aNGr8" alt=""><figcaption></figcaption></figure>

修改好後，直接執行 git add 及 git commit 相關指令即可，即完成合併：

<figure><img src="/files/P2WK0EbDOFpoyFr2072p" alt=""><figcaption></figcaption></figure>

## 練習

創造衝突：針對同一個檔案，在兩個 branch 當中，改同一行程式，然後進行合併。

解決衝突，並進行合併提交。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.webmix.cc/git/4-git-ji-ben-cao-zuo/4.14-he-bing-yu-dao-chong-tu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
