Skip to content

firehourse/gemini-ai-commit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gemini-ai-commit: AI-Powered Git Commit Generator

由 Google Gemini 驅動的 Git Commit Message 自動生成工具。分析 Staged Diff,依 Conventional Commits 規範輸出精準的提交訊息,支援自訂模型、語言與 Prompt 模板。

安裝

方式一:一鍵安裝(推薦)

自動偵測 OS/Arch,從 GitHub Releases 下載 binary 至 ~/.local/bin

curl -sSL https://raw.githubusercontent.com/firehourse/gemini-ai-commit/main/install.sh | bash

方式二:go install

已有 Go 環境的使用者可直接安裝:

go install github.com/firehourse/gemini-ai-commit@latest

方式三:手動下載

前往 Releases 頁面,下載對應平台的 binary,解壓後放入 PATH 中即可。

方式四:從原始碼編譯(開發者)

git clone https://github.com/firehourse/gemini-ai-commit.git
cd gemini-ai-commit
make install   # 編譯並安裝至 ~/.local/bin/

設定環境

確認 ~/.local/bin 在 PATH 中,並配置 API Key:

# 加入 PATH(如尚未加入)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

# 配置 Gemini API Key
echo 'export GEMINI_API_KEY="your-api-key-here"' >> ~/.bashrc

# 建議加入 alias 方便呼叫
echo "alias ai='gemini-ai-commit'" >> ~/.bashrc

source ~/.bashrc

3. 使用方式

# Stage 你的變更
git add .

# 生成 commit message 並互動確認
ai

# 或完整指令
gemini-ai-commit

使用參數

Usage:
  gemini-ai-commit [flags]

Flags:
  -l, --lang string     commit 訊息語言,如 en、zh-TW (預設: en)
  -m, --model string    指定 Gemini 模型 (預設: gemini-2.5-flash-lite)
  -p, --prompt string   自訂 prompt 模板路徑 (.tmpl)
      --dry-run         僅印出 commit 訊息,不執行 git commit
  -h, --help            顯示說明

範例:

# 中文 commit message
ai --lang zh-TW

# 使用其他 Gemini 模型
ai --model gemini-2.0-flash

# 使用自訂 prompt 模板
ai --prompt ./prompts/my-style.tmpl

# 預覽模式,不實際 commit
ai --dry-run

自訂 Prompt 模板

工具內建預設模板,也可傳入自定義 .tmpl 檔案。模板使用 Go text/template 語法,支援以下變數:

變數 說明
{{.Diff}} Staged 的 git diff 內容
{{.Language}} 透過 --lang 指定的語言

模板範例:

You are a senior engineer. Write a commit message for the following diff.
Use Conventional Commits format. Language: {{.Language}}.
Only output the message.

Diff:
{{.Diff}}

執行流程

git add .  →  gemini-ai-commit
    │
    ├── 1. 驗證當前目錄是否為 Git Repo
    ├── 2. 讀取 Staged Diff (git diff --cached)
    ├── 3. 渲染 Prompt 模板(內建或自訂)
    ├── 4. 呼叫 Gemini API 生成 commit message
    ├── 5. 顯示建議訊息,等待使用者確認 [Y/n]
    └── 6. 執行 git commit -m "<message>"

專案結構

gemini-ai-commit/
├── main.go                  # 程式入口
├── cmd/
│   └── root.go              # cobra CLI 指令定義與執行邏輯
├── pkg/
│   ├── ai/gemini.go         # Gemini API 客戶端封裝
│   ├── git/git.go           # Git 操作工具(diff 讀取、repo 驗證)
│   └── prompt/prompt.go     # Prompt 模板渲染引擎
├── prompts/
│   └── default.tmpl         # 預設 prompt 模板(備用)
├── Makefile                 # make build / make install
└── go.mod / go.sum          # Go 依賴管理

依賴項目

About

ai commit tool for git

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors