# VS Code Github Copilot 自定義 Commit Message
## 直接調整 VS Code 延伸模組設定檔 `settings.json`
最簡單快速的方法就是直接去 VS Code 的 `settings.json` 調整設定。
~~但如果有其他需求,下方提供[另一種做法](#%E4%BD%BF%E7%94%A8-copilot-instructionsmd-%E6%96%87%E4%BB%B6-%E9%80%B2%E8%A1%8C%E8%A9%B3%E7%B4%B0%E8%A8%AD%E5%AE%9A)。~~
2025.01.08說明: 目前不建議這個做法,因為這個設定吃不到
### VS Code Instruction設定位置
如果有安裝Github Copilot延伸模組,可以設定自定義的Commit Message,在VS Code的設定中尋找 `延伸模組 > GitHub Copilot Chat 延伸模組`。
點選在`settings.json` 內編輯,或自己在檔案中新增 attribute: `github.copilot.chat.commitMessageGeneration.instructions` 參數來定義生成 Commit Message 的規則。

這些規則可以包括訊息的語言、格式、類型以及內容長度等,本質就是用提示工程引導 Github Copilot 協助產生符合需求的 commit message 。
### 設定規範
只有兩種設定格式:
若要直接在設定中定義指令,使用 `text` 屬性。若要引用外部文件,使用 `file` 屬性。
### 設定範本
以下是我的設定
`settings.json`
```json
{
// ... other settings
"github.copilot.chat.commitMessageGeneration.instructions": [
{ "text": "Write commit messages in English." },
{ "text": "Follow the structure: `<type>(<scope>): <subject>`." },
{
"text": "Use the following types: feat, fix, docs, style, refactor, perf, test, chore, revert."
},
{ "text": "Keep the subject concise (max 50 characters, no period)." },
{
"text": "Provide a detailed body explaining the changes (wrap lines at 72 characters)."
}
]
// ... other settings
}
```
效果如圖附圖:

## ~~使用 `copilot-instructions.md` 文件 進行詳細設定~~
(不建議使用,IDE抓取不到此檔案)
### 在 `.github` 資料夾中建立 `copilot-instructions.md` 文件
如果想要設定的Commit Message有比較複雜且嚴謹的描述,可以考慮採用這個方法:
1. 在工作區新增 `.github` 資料夾
2. 在該資料夾下建立 `copilot-instructions.md`
3. 用自然語言的方式寫關於 Commit Message 的描述,aka用講人話的方式寫規範。
額外補充:
- 註1: 如果同時在VS Code設定中已有設定檔,又另外建立了上述的文件,Copilot會嘗試合併兩個文件的描述。
- 註2: 檔案中的換行、空格在送入Github Copilot時會被忽略,因此文件寫得讓人看得懂即可。
- 註3: 這個檔案與 Visual Studio 2022 參考的設定檔位置是相同的,這對於開發dotnet專案很方便,兩邊IDE共用同一個描述檔。
- 註4: 目前不論是 VS Code 與 Visual Studio 2022 IDE 似乎都沒有吃到這個 `copilot-instructions.md` 檔案,感覺版本並不穩定,不建議使用。
### 設定範本
`copilot-instructions.md`
```md
First line should be a short header of the change less than 50 characters. The header in the format `<type>(<scope>): <subject>`.
Use types like feat ONLY in features, fix for bug fixes, docs for documentation, style for formatting, refactor for restructuring, perf for performance, test for tests, chore for maintenance, or revert for rollbacks.
Include scope to specify the affected area.
Keep the subject under 50 characters.
The second line should be blank.
The third line should start with full summary of the change, explain the change in detail, including the problem, solution, and context, wrapping lines at 72 characters.
The message must only in English
```
## 2025.01.08 補充 template 範本
這是我目前遇到格式很漂亮,commit message又很精準的prompt,不想產生body的話把最後一行移除即可。
```json
{
"github.copilot.chat.commitMessageGeneration.instructions": [
{
"text": "Write a concise commit message from 'git diff --staged' output in the format `[EMOJI] [TYPE](file/topic): [description in {locale}]`. Use GitMoji emojis (e.g., ✨ → feat), present tense, active voice, max 240 characters per line, no code blocks."
},
{
"text": "Present tense, active voice, max 240 characters per line, no code blocks."
},
{
"text": "Provide a detailed body explaining the changes (wrap lines less then 72 characters)."
}
],
}
```
### 文章參考
https://code.visualstudio.com/docs/copilot/copilot-customization
P.S. 內文還有提及關於自定義測試產生指令以及其他指令設定,方法都類似上述做法,有興趣可以點進連結查閱。