> For the complete documentation index, see [llms.txt](https://docs.xiwind-corp.com/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xiwind-corp.com/tech/git/branch-ru-men.md).

# Branch 入門

## **更新 Repositors**

***

1. **使用 Remote Repository 更新本地 Repository。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git pull
```

{% endcode %}

{% hint style="info" %}
**使用時機。**

1. 如果您正在與其他人協作完成一個項目並且他們對 Remote Repository 進行了更改。
2. 如果您正在從事一個長期項目，而其他人經常對 Remote Repository 進行更改。
3. 如果您和其他人對存儲庫中的同一文件進行了更改，則在嘗試合併更改時可能會遇到衝突
   {% endhint %}

<figure><img src="/files/3CA9YGeu25CSY87PF2aU" alt="" width="375"><figcaption></figcaption></figure>

2. **查看 commit 歷程檔案。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git log
```

{% endcode %}

<figure><img src="/files/zcyJLGsdpstfhpRZTCnE" alt="" width="375"><figcaption></figcaption></figure>

## Branch

***

Git 的關鍵特性之一是它支援 Branch(分支)，它允許開發人員同時處理 Repository 的多個版本。Git 分支是一條獨立的開發線，與主要程式不同。 Git 中的主 Branch 通常稱為 "master" 或 "main"，對 Repository 的所有更改都在主 Branch 上進行。

<figure><img src="/files/r0jkKLIqNkjU6UD3a3HY" alt="" width="375"><figcaption></figcaption></figure>

開發人員可以創建新 Branch 來開發特定功能或修復 Bug。

* **Feature branch**: 是一種特定類型的 Git Branch，用於開發新特性或功能。 當開發人員創建一個新的 Feature branch 時，通常以正在處理的功能命名它，然後在該分支上工作直到該功能完成。 功能分支與主分支隔離，這意味著在 Feature branch 上所做的任何更改都不會影響主 Branch，直到它們被 Merged(合併)。

1. **複製主要的 Branch 並建立 Branch。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git branch <Branch 名稱>
git checkout <Branch 名稱>
```

{% endcode %}

<figure><img src="/files/E32Jsp2yOGtJLmAzf5HO" alt="" width="375"><figcaption></figcaption></figure>

2. **查看 Commit (提交) 歷史。** (僅顯示 Commit 標題和摘要)

{% code title="SHELL" overflow="wrap" %}

```powershell
git log --oneline
```

{% endcode %}

<figure><img src="/files/6CJhH3Fd6fKDZjzBMKRA" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %}
**Git SHA-1 ID**: 51be1c0&#x20;

**目前指向的 Branch (HEAD)**: Branch001
{% endhint %}

3. **首次 Push Branch。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git push --set-upstream origin
```

{% endcode %}

4. **顯示當前 Branch 上最近 Commit 的詳細訊息。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git show
git show <Git SHA-1 ID>
```

{% endcode %}

<figure><img src="/files/xFK9UAJzYMcxPWdxC2gy" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %} <mark style="color:red;">紅色標記</mark>: 刪除。 <mark style="color:green;">綠色標記</mark>: 新增。 <mark style="color:yellow;">黃色標記</mark>: 修改。
{% endhint %}

5. **將 Branch Merge (合併) 到當前 Branch。**(又稱 Merge commit)

{% code title="SHELL" overflow="wrap" %}

```powershell
git merge <要合併的 Branch>
```

{% endcode %}

{% hint style="success" %}
**必須先將更改 Commit 或 Stash。(如果有的話)。**
{% endhint %}

### Merge conflict

***

當 Git 無法自動解決正在 Merge 在一起的兩個或多個分支之間的差異時，就會發生Merge conflict(合併衝突)。 例如：兩個或多個分支以不同方式對同一文件進行更改時，可能會發生這種情況，這樣 Git 無法確定應優先進行哪些更改。

當 Git 檢測到合併衝突時將會停止合併，並指出哪些文件存在 Conflict。 然後由使用者透過手動編輯衝突文件來解決 Conflict。

<figure><img src="/files/bTNUiRljLXOvn4RXr2C5" alt="" width="375"><figcaption></figcaption></figure>

### Merge tool

使用 Merge tool 可以解決 Command line 介面衝突的問題。(預設使用 Vim) 例如: [P4Merge](https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge)、[KDiff3](https://sourceforge.net/projects/kdiff3/files/)

1. **安裝 Merge tool。**

<figure><img src="/files/ONCZKJZFQD2RhLrCoh2r" alt="" width="318"><figcaption></figcaption></figure>

2. **設定 Merge tool。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git config --global merge.tool p4merge
```

{% endcode %}

3. **開啟 Merge tool。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git mergetool
```

{% endcode %}

<div><figure><img src="/files/TYQMpvZ53AzGXydkUjIe" alt="" width="375"><figcaption></figcaption></figure> <figure><img src="/files/71i06QGjG2KHCXGmEQq4" alt="" width="188"><figcaption><p>P4Merge 畫面</p></figcaption></figure> <figure><img src="/files/hmBFFGckxPKb4oLf5zY3" alt="" width="375"><figcaption><p>Vim 畫面</p></figcaption></figure></div>

4. **執行因 Conflict 而暫停的 Merge。** ( (含 Commit，會開啟編輯器編輯 Merge Commit 資訊)

{% code title="SHELL" overflow="wrap" %}

```powershell
git merge --continue
```

{% endcode %}

<figure><img src="/files/hDxsN2FnQ0HQGorCo9TI" alt="" width="375"><figcaption></figcaption></figure>

## Pull Request

***

Pull Request(拉取請求)是一項功能，允許開發人員將更改的內容 Commit 到 Remote Repository 上，以供審查和集成到主要的程式碼中。

當開發者想為一個項目貢獻時，他們首先在 Git Repository 中創建一個新的 Branch，並在該 Branch 上進行修改。Pull Request 通常包括所做更改的摘要、任何相關註釋或文檔，以及代碼更改的差異或比較。

一旦貢獻的內容完成，開發人員就可以提交 Pull Request，要求 Repository 維護者 Pull Branch 檢視並將它們 Merge 到 Main branch 中。

<figure><img src="/files/kmpTncyvnQnocdmyYcZT" alt="" width="375"><figcaption><p>Branch</p></figcaption></figure>

Repository 的維護者可以審查更改，並決定是接受還是拒絕 Pull Request 。如果接受，更改將合併到 Main branch 中，如果拒絕，開發人員可以修改他們的更改並重新提交 Pull Request。

{% hint style="success" %}
**Pull Request 可以幫助維護 Repository 的完整性和穩定性，因為更改在 Merge 到 Main branch 之前，需要經過審查。**
{% endhint %}

1. **點擊和發布 Pull Request。**

<div><figure><img src="/files/mQ4skAkWrj92b8b1tFxB" alt="" width="375"><figcaption><p>點擊 Request Push</p></figcaption></figure> <figure><img src="/files/Q1VOn19MjsQwPjkD1t6p" alt="" width="375"><figcaption><p>選擇 Branch</p></figcaption></figure></div>

2. **預覽和比較(Comparing)變更，最後確定建立 Pull Request。**

<figure><img src="/files/C8XFX6Qh9ksgqgb5ifcI" alt="" width="375"><figcaption><p>預覽變更</p></figcaption></figure>

3. **編輯 Pull Request 資訊。**

* **Reviewers(審閱者)**：是負責審閱 Pull Request，並向 Assignees 提供反饋。審閱者可以對提議的 Pull Request 進行修改或請求開發者進行其他更改。
* **Assignees(受讓者)**：是負責完成 Pull Request 和 Merge。

<figure><img src="/files/1niVXACzBBNCQOXR3NfX" alt="" width="375"><figcaption></figcaption></figure>

4. **查看 Pull Request。**

<div><figure><img src="/files/RgfPaGkN1hL4dDH7SRDw" alt=""><figcaption><p>點擊 Pull Request 列表</p></figcaption></figure> <figure><img src="/files/UiSdzfAfutYUK6seLlw1" alt=""><figcaption><p>查看 Pull Request 內容</p></figcaption></figure></div>

5. **審查 Pull Request，並提供意見。** (必須擁有權限)

<figure><img src="/files/yp5wpoFv1fxC9GRm46Vg" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %}
**Require approval from specific reviewers before merging**：增設 Repository 保護規則。例如：需要特定 Reviewers 的批准才能將 Merge 到Main branch 中。&#x20;

**Continuous integration has not been setup**：此 Repository 沒有設置持續集成 (CI) 系統。 CI 系統自動執行構建、測試和部署程式碼更改的過程，這有助於及早發現錯誤並確保更改與項目的其餘部分順利集成。&#x20;

**This branch has no conflicts with the base branch**：如果您看到此消息，則意味著，應該能夠自動 Merge，而無需解決任何 Conflicts。
{% endhint %}

6. **審查結束，選擇 Merge 模式，進行 Merge。**

<div><figure><img src="/files/wSYZncbeYt6ncjL0clky" alt=""><figcaption><p>選擇 Merge 模式</p></figcaption></figure> <figure><img src="/files/cRv8HZTV2EQLqKO1lAC0" alt=""><figcaption><p>填寫 Merge 資訊</p></figcaption></figure> <figure><img src="/files/rkqUAsonTMeLiI9BlLTq" alt=""><figcaption><p>Merge 後</p></figcaption></figure></div>

7. **更新本地 Repository。**

{% code title="SHELL" overflow="wrap" %}

```SHELL
git pull
```

{% endcode %}

## 參考資料

***

[Windows 上的 Git：如何設置合併工具？ - 堆棧溢出](https://stackoverflow.com/questions/426026/git-on-windows-how-do-you-set-up-a-mergetool)

[Git and GitHub - The Game Changer](https://pavankumar07.hashnode.dev/git-and-github-the-game-changer)
