> 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/e-wai-gong-neng.md).

# 額外功能

## Note

***

Note(註釋)允許您附加額外訊息或註釋附加到 Git 物件(Commit, Tag, 或 Tree)，用於提供解釋和參考。

1. **新增 Note。**

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

```powershell
git notes add <Git SHA-1 ID>
git notes add -m '<Note 註解>' <Git SHA-1 ID>
```

{% endcode %}

`git notes add`: 使用預設編輯器編輯 Note&#x20;

`git notes add -m`: 直接編輯 Note

<div><figure><img src="/files/gBzyyBpW35WY7d983hug" alt=""><figcaption><p>選擇 Commit</p></figcaption></figure> <figure><img src="/files/qXu82oeT8YeRE9ErEw8Z" alt=""><figcaption><p>新增 Note</p></figcaption></figure></div>

2. **查看 Note。**

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

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

{% endcode %}

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

```SHELL
git log --show-notes
```

{% endcode %}

<div><figure><img src="/files/V4T53Xs0WBUdNUKxSGZX" alt=""><figcaption><p>notes show</p></figcaption></figure> <figure><img src="/files/MpJVoHfhyomxOS6XkUxD" alt=""><figcaption><p>--show-notes</p></figcaption></figure></div>

## Tag

***

Tag (標籤)用於標記 Git Commit 歷史中重要的里程碑。例如：對外發布、版本變更或重要 Commit。

1. **新增 Note。**

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

```powershell
git tag <Tag 名稱> 
git tag <Tag 名稱> <Git SHA-1 ID>
```

{% endcode %}

<div><figure><img src="/files/DFhPPZNNYeJwqsFBmLqT" alt=""><figcaption><p>選擇 Commit</p></figcaption></figure> <figure><img src="/files/7243BeuhzeYgcWaY9lze" alt=""><figcaption><p>新增 Tag</p></figcaption></figure></div>

2. **查看 Tag。**

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

```SHELL
git log --oneline
```

{% endcode %}

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

3. **新增含註解的 Tag。** (例如: 標籤製作者、電子郵件、日期、註解...)

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

```SHELL
git tag -a <Tag 名稱> -m "<Tag 註解>"
git tag -a <Tag 名稱> <Git SHA-1 ID> -m "<Tag 註解>"
```

{% endcode %}

<div><figure><img src="/files/XMN0IYWqyDXcE1pZ2Sfn" alt=""><figcaption><p>新增 Tag</p></figcaption></figure> <figure><img src="/files/GzphFu4ym0gCJywgW8Ir" alt=""><figcaption><p>查看 Tag</p></figcaption></figure></div>

4. **修改 Tag 指向。** (當設置錯誤的 Commit 時)

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

```SHELL
git tag -f <Tag 名稱> <Git SHA-1 ID>
```

{% endcode %}

<div><figure><img src="/files/sNawms6Gw3vzOiKRjh65" alt=""><figcaption><p>修改 Tag</p></figcaption></figure> <figure><img src="/files/Evb3o6f8HMzrg8hXbHcZ" alt=""><figcaption><p>查看 Tag</p></figcaption></figure></div>

5. **查看指定 Tag Commit。**

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

```SHELL
git show <Tag 名稱>
```

{% endcode %}

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

7. **刪除Tag。**

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

```SHELL
git tag -d <Tag 名稱>
```

{% endcode %}

8. **您使用將更改 Push 到 Remote repository 時，不會自動推送 Tag。您需要明確指定選項`--tags`。**

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

```SHELL
git push --tags
```

{% endcode %}

## Alias

***

Alias(別名) 功能允許您為常用的 Git 命令建立自定義的縮寫，從而幫助您節時間。

無需鍵入整個命令，您只需使用別名即可獲得相同的結果。可讓您的 Git 體驗更高效並根據您的特定需求量身定制。

1. **建立別名。** (同一命令可以建立多筆 Alias)

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

```SHELL
git config --global alias.<別名> <git 指令>
git config --global alias.st status
git config --global alias.bc checkout -b
git config --global alias.cb checkout -b
git config --global alias.cam 'commit -a -m'
git config --global alias.nx 'log --name-only --oneline'
```

{% endcode %}

{% hint style="info" %}
**同一命令可以建立多筆 Alias。多個參數必須使用: " " or ' '。**
{% endhint %}

2. **開啟設定打查看或編輯。**(使用預設編輯器打開)

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

```SHELL
git config --edit --global
```

{% endcode %}

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

## 進階 Log

***

1. **建立更好看的 log 別名。** `紅色 Git SHA-1 ID,黃色 Tag 名稱, 綠色 Commit Time, Commit 作者`

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

```powershell
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset-%C(yellow)%d%Creset %s %Cgreen(%cr) %C(yellow)<%an>%Creset' --abbrev-commit"
```

{% endcode %}

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

```SHELL
git lg
```

{% endcode %}

<div><figure><img src="/files/lipg2BkThQRz9qK31f9h" alt=""><figcaption><p>原始 log</p></figcaption></figure> <figure><img src="/files/IPTKjNsRUh7etCJE2mX3" alt=""><figcaption><p>修改後的 log</p></figcaption></figure></div>

2. 使用條件查找 Log 。(包含特定字串、Commit 作者...)

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

```powershell
git lg -S<字串>
git lg --committer="<Commit 作者>"
git lg --since='<時間> <時間單位>'
```

{% endcode %}

`<時間單位>`: minute, hour, day, week, month, year

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

## Diff

***

1. **比對尚未進入 Staging area 的檔案的差異。或是指定比對。**

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

```powershell
git diff 
git diff <檔案名稱>
git diff <Git SHA-1 ID>
git diff <Git SHA-1 ID> <Git SHA-1 ID>
git diff <Branch 名稱> <Branch 名稱>
```

{% endcode %}

`--cached`: 比對已進入 Staging area 的檔案。&#x20;

`---stat`: 僅顯示差異摘要。&#x20;

`>*.txt`: 將比對結果輸出為文字檔。

<figure><img src="/files/jsbtpwYAHmIW0pNYsTfS" 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 %}

### Blame

找出文件每一行的作者。它提供文件的逐行註釋，Commit 、作者姓名和每行的最後修改日期。

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

```powershell
git blame <檔案>
```

{% endcode %}

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

### Bisect

bisect(二進制搜索) 可幫助您找到錯誤的(壞的) Commit，並回復未修改前的(好的) Commit。 它透過 Commit 歷史執行二進制搜索，縮小可能的 Commit 範圍來定位錯誤的 Commit。

<figure><img src="/files/TsPLty1OCPFMJLdWetyy" alt="" width="360"><figcaption></figcaption></figure>

<figure><img src="/files/nWZxFdIFj8gec61b86zX" alt="" width="329"><figcaption></figcaption></figure>

在每個步驟中，會消除剩餘一半的 Commit，讓您可以高效地定位有問題的Commit。它自動縮小錯誤的 Commit 的範圍。

1. **啟動 bisect。**

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

```powershell
git bisect start
git bisect good <已知的壞的 Git SHA-1 ID> <已知的好的 Git SHA-1 ID>
```

{% endcode %}

2. **此時，您需要檢查或運行程式碼並確定錯誤是否存在。** (Git 會自動 Checkout Commit 供您測試)<br>
3. **將當前 Commit 標記為好的或壞的。**

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

```powershell
git bisect good
git bisect bad
```

{% endcode %}

4. **找到問題壞的 Commit時，退出 bisect。**

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

```powershell
git bisect reset
```

{% endcode %}

## 參考資料

***

[利用git diff 來為版本控制專案找出差異](https://www.tpisoftware.com/tpu/articleDetails/1450)

[Who Broke My Test? A Git Bisect Tutorial | Sumo Logic](https://www.sumologic.com/blog/who-broke-my-test-a-git-bisect-tutorial/)

[使用 Git Bisect 快速找到第一個有問題的 Commit | Puck's Blog](https://blog.puckwang.com/posts/2021/use-git-bisect-debug/)

[Puck's Blog](https://blog.puckwang.com/)
