一个用 Go 编写的 CLI 工具,它使用 ChatGPT AI(gpt-4o,gpt-4 模型)为你编写 git 提交信息或提供代码审查摘要,并自动安装 git prepare-commit-msg hook。
- 支持 Azure OpenAI Service、OpenAI API、Gemini、Anthropic、Ollama、Groq 和 OpenRouter。
- 遵循 conventional commits 规范。
- 集成 Git prepare-commit-msg Hook,详见 Git Hooks 文档。
- 可通过指定上下文行数(默认值:3)自定义生成的差异内容。
- 支持差异文件排除模式(git diff)。
- 可将提交信息翻译为其他语言(支持
en、zh-tw、zh-cn)。 - 支持 SOCKS 代理或自定义网络 HTTP 代理。
- 生成简洁的代码审查摘要。
- 支持自定义提示模板和变量。

通过 Homebrew 安装:
brew tap appleboy/tap
brew install codegpt通过 Chocolatey 安装:
choco install codegpt大多数系统推荐使用自动化脚本安装(推荐),也可手动下载二进制文件。
执行脚本,自动下载安装最新版:
bash < <(curl -sSL https://raw.githubusercontent.com/appleboy/CodeGPT/main/install.sh)或手动下载脚本后执行:
curl -LO https://raw.githubusercontent.com/appleboy/CodeGPT/main/install.sh
chmod +x install.sh
./install.sh-
从 发布页面 下载最新二进制文件。
-
修改权限:
chmod 755 codegpt
-
移动到 bin 目录:
mv codegpt /usr/local/bin/
-
验证安装:
codegpt version
| 变量名 | 默认值 | 说明 |
|---|---|---|
| VERSION | latest | 要安装的 CodeGPT 版本(默认为最新发布版) |
| INSTALL_DIR | $HOME/.codegpt/bin | 安装目录 |
| INSECURE | 未设置(默认关闭) | 只要设置该变量(值不限),即跳过 SSL 验证 |
使用示例:
# 启用 insecure 模式(跳过 curl SSL 验证)
INSECURE=1 ./install.sh
# 安装指定版本到自定义目录
VERSION=1.1.0 INSTALL_DIR=/opt/codegpt ./install.sh该脚本会:
- 检测您的操作系统和架构
- 下载最新版二进制文件
- 将程序加入您的 PATH
从源码安装:
go install github.com/appleboy/CodeGPT/cmd/codegpt@latest在 devcontainer.json 中添加 feature:
"features": {
"ghcr.io/kvokka/features/codegpt:1": {}
}请先创建你的 OpenAI API Key。可以在 OpenAI 平台 生成新的 API Key。
环境变量是设置在操作系统上的变量,而不是在应用程序内。它由名称和值组成。我们建议将变量名称设置为 OPENAI_API_KEY。
参见 API Key 安全最佳实践。
export OPENAI_API_KEY=sk-xxxxxxx或者将你的 API key 存储在自定义配置文件中。
codegpt config set openai.api_key sk-xxxxxxx这将在你的主目录中创建一个 .codegpt.yaml 文件($HOME/.config/codegpt/.codegpt.yaml)。以下选项可用。
| 选项 | 描述 |
|---|---|
| openai.base_url | 替换默认的基本 URL (https://api.openai.com/v1)。 |
| openai.api_key | 从 openai 平台页面 生成 API key。 |
| openai.api_key_helper | 用于动态生成 API key 的 Shell 命令(例如从密码管理器或密钥服务获取)。 |
| openai.api_key_helper_refresh_interval | 从 api_key_helper 刷新凭证的间隔秒数(默认:900 秒 / 15 分钟)。设置为 0 以禁用缓存。 |
| openai.org_id | 在 API 请求中有时使用的组织标识符。参见 组织设置。仅适用于 openai 服务。 |
| openai.model | 默认模型是 gpt-4o,你可以更改为其他自定义模型(Groq 或 OpenRouter 提供)。 |
| openai.proxy | HTTP/HTTPS 客户端代理。 |
| openai.socks | SOCKS 客户端代理。 |
| openai.timeout | 默认 HTTP 超时时间是 10s(十秒)。 |
| openai.skip_verify | 默认 skip_verify 设置为 false,可以将其更改为 true 以忽略 SSL 验证。 |
| openai.max_tokens | 默认最大 token 数是 300。参见参考 max_tokens。 |
| openai.temperature | 默认温度是 1。参见参考 temperature。 |
| git.diff_unified | 生成具有 <n> 行上下文的差异,默认是 3。 |
| git.exclude_list | 从 git diff 命令中排除文件。 |
| openai.provider | 默认服务提供商是 openai,你可以更改为 azure。 |
| output.lang | 默认语言是 en,可用语言有 zh-tw、zh-cn、ja。 |
| openai.top_p | 默认 top_p 是 1.0。参见参考 top_p。 |
| openai.frequency_penalty | 默认 frequency_penalty 是 0.0。参见参考 frequency_penalty。 |
| openai.presence_penalty | 默认 presence_penalty 是 0.0。参见参考 presence_penalty。 |
| prompt.folder | 默认提示文件夹是 $HOME/.config/codegpt/prompt。 |
你可以使用 Shell 命令从密码管理器或密钥服务动态获取 API key,而不是直接将其存储在配置文件中。这特别适用于:
- 从密码管理器获取密钥(1Password、Bitwarden 等)
- 使用云端密钥服务(AWS Secrets Manager、Google Secret Manager 等)
- 实现密钥轮换和短期凭证
- 提高安全性,避免以明文存储密钥
配置 Shell 命令来获取你的 API key:
# 使用 1Password CLI
codegpt config set openai.api_key_helper "op read op://vault/openai/api_key"
# 使用 AWS Secrets Manager
codegpt config set openai.api_key_helper "aws secretsmanager get-secret-value --secret-id openai-key --query SecretString --output text"
# 使用 Google Cloud Secret Manager
codegpt config set openai.api_key_helper "gcloud secrets versions access latest --secret=openai-api-key"
# 使用环境变量
codegpt config set openai.api_key_helper "echo \$MY_OPENAI_KEY"
# 自定义脚本
codegpt config set openai.api_key_helper "/path/to/get-api-key.sh"默认情况下,API key 会被缓存 15 分钟(900 秒),以避免对密钥服务的过度调用:
# 设置刷新间隔为 5 分钟
codegpt config set openai.api_key_helper_refresh_interval 300
# 设置刷新间隔为 30 分钟
codegpt config set openai.api_key_helper_refresh_interval 1800
# 禁用缓存(每次都重新获取密钥)
codegpt config set openai.api_key_helper_refresh_interval 0对于 Gemini 提供者,你可以设置单独的 helper:
codegpt config set gemini.api_key_helper "gcloud secrets versions access latest --secret=gemini-key"
codegpt config set gemini.api_key_helper_refresh_interval 600- 首次执行:CodeGPT 运行你的 helper 命令并将 API key 缓存到
~/.config/codegpt/.cache/,文件权限为 0600(仅所有者可读写) - 后续执行:在刷新间隔内,CodeGPT 使用缓存的密钥
- 过期后:CodeGPT 自动重新运行 helper 命令并更新缓存
- 安全性:缓存文件以仅所有者可读写的权限存储
当配置了多个 API key 来源时,CodeGPT 使用以下优先顺序:
openai.api_key_helper(如果已配置)openai.api_key(静态配置)OPENAI_API_KEY环境变量
默认提示文件夹位于 $HOME/.config/codegpt/prompt。你可以通过执行以下命令将其更改为其他目录:
codegpt config set prompt.folder /path/to/your/prompt要从自定义文件夹加载提示文件,请运行:
codegpt prompt --load执行后,你将看到类似以下的消息:
save code_review_file_diff.tmpl to /Users/xxxxx/.config/codegpt/prompt/code_review_file_diff.tmpl
save summarize_file_diff.tmpl to /Users/xxxxx/.config/codegpt/prompt/summarize_file_diff.tmpl
save summarize_title.tmpl to /Users/xxxxx/.config/codegpt/prompt/summarize_title.tmpl
save conventional_commit.tmpl to /Users/xxxxx/.config/codegpt/prompt/conventional_commit.tmpl请从 Azure 资源管理门户左侧菜单获取 API key、Endpoint 和 Model deployments 列表。
更新你的配置文件。
codegpt config set openai.provider azure
codegpt config set openai.base_url https://xxxxxxxxx.openai.azure.com/
codegpt config set openai.api_key xxxxxxxxxxxxxxxx
codegpt config set openai.model xxxxx-gpt-4o支持 Gemini API 服务
你可以使用 Gemini API 或 VertexAI Gemini 服务。参见 Gemini API 文档 和 VertexAI 文档。
请在配置文件中更新以下参数。
- 请从 Gemini API 页面(用于 BackendGeminiAPI)或 VertexAI API Key(用于 BackendVertexAI)创建 API key。
| 选项 | 描述 | 示例值 | 是否必填 | 默认值 |
|---|---|---|---|---|
| openai.provider | 设置为 gemini 以使用 Gemini 提供商 |
gemini |
是 | |
| gemini.api_key | Gemini 或 VertexAI 的 API key | xxxxxxx |
是 | |
| gemini.model | 模型名称(参见 Gemini 模型) | gemini-2.0-flash |
是 | |
| gemini.backend | Gemini 后端:BackendGeminiAPI(默认,适用于 Gemini API)或 BackendVertexAI(VertexAI) |
BackendGeminiAPI |
否 | BackendGeminiAPI |
| gemini.project_id | VertexAI 项目 ID(如使用 BackendVertexAI 必填) |
my-gcp-project |
条件必填 | |
| gemini.location | VertexAI 区域(如使用 BackendVertexAI 必填) |
us-central1 |
条件必填 |
codegpt config set openai.provider gemini
codegpt config set gemini.api_key xxxxxxx
codegpt config set openai.model gemini-2.0-flash
# gemini.backend 默认为 BackendGeminiAPI,可省略codegpt config set openai.provider gemini
codegpt config set openai.model gemini-1.5-pro-preview-0409
codegpt config set gemini.backend BackendVertexAI
codegpt config set gemini.project_id my-gcp-project
codegpt config set gemini.location us-central1flowchart TD
User([用户])
subgraph CodeGPT
GeminiClient([Gemini 提供商])
end
subgraph Google
GeminiAPI([Gemini API])
VertexAI([VertexAI Gemini])
end
User -->|Completion / GetSummaryPrefix| GeminiClient
GeminiClient -- BackendGeminiAPI --> GeminiAPI
GeminiAPI -- Response (text, usage) --> GeminiClient
GeminiClient -- BackendVertexAI --> VertexAI
VertexAI -- Response (text, usage) --> GeminiClient
GeminiClient --> User
支持 Anthropic API 服务
使用 Anthropic API 构建,你可以参见 Anthropic API 文档。在你的配置文件中更新 provider 和 api_key。请从 Anthropic API 页面创建 API key。
codegpt config set openai.provider anthropic
codegpt config set openai.api_key xxxxxxx
codegpt config set openai.model claude-3-5-sonnet-20241022参见 Anthropic API 文档 中的模型列表。
如何切换到 Groq API 服务
请从 Groq API 服务获取 API key,请访问 这里。在你的配置文件中更新 base_url 和 api_key。
codegpt config set openai.provider openai
codegpt config set openai.base_url https://api.groq.com/openai/v1
codegpt config set openai.api_key gsk_xxxxxxxxxxxxxx
codegpt config set openai.model llama3-8b-8192GroqCloud 目前支持 以下模型:
我们可以使用 ollama API 服务中的 Llama3 模型,请访问 这里。在你的配置文件中更新 base_url。
# 拉取 llama3 8b 模型
ollama pull llama3
ollama cp llama3 gpt-4o尝试使用 ollama API 服务。
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'在你的配置文件中更新 base_url。你不需要在配置文件中设置 api_key。
codegpt config set openai.base_url http://localhost:11434/v1如何切换到 OpenRouter API 服务
你可以参见 支持的模型列表,模型使用可以由用户、开发者或两者支付,并且可能会在 可用性 上有所变化。你还可以通过 API 获取模型、价格和限制。
以下示例使用免费模型名称:meta-llama/llama-3-8b-instruct:free
codegpt config set openai.provider openai
codegpt config set openai.base_url https://openrouter.ai/api/v1
codegpt config set openai.api_key sk-or-v1-xxxxxxxxxxxxxxxx
codegpt config set openai.model google/gemini-3-flash-preview要将你的应用程序包含在 openrouter.ai 排名中并在 openrouter.ai 排名中显示,你可以在配置文件中设置 openai.headers。
codegpt config set openai.headers "HTTP-Referer=https://github.com/appleboy/CodeGPT X-Title=CodeGPT"- HTTP-Refer:可选,用于将你的应用程序包含在 openrouter.ai 排名中。
- X-Title:可选,用于在 openrouter.ai 排名中显示。
有两种方法可以使用 codegpt 命令生成提交信息:CLI 模式和 Git Hook。
你可以直接调用 codegpt 为你的暂存更改生成提交信息:
git add <files...>
codegpt commit --preview提交信息如下所示。
使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
================提交摘要====================
feat: 在 commit 命令和模板文件中添加预览标志并移除 disableCommit 标志。
- 在 `commit` 命令中添加 `preview` 标志
- 从 `prepare-commit-msg` 模板文件中移除 `disbaleCommit` 标志
==================================================
将提交信息写入 .git/COMMIT_EDITMSG 文件或将所有 git 提交信息翻译成其他语言(繁体中文、简体中文或 日语)
codegpt commit --lang zh-tw --preview考虑以下结果:
使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
我们正在尝试将 git 提交信息翻译成繁体中文
================提交摘要====================
功能:重构 codegpt commit 命令标记
- 将「codegpt commit」命令新增「预览」标记
- 从「codegpt commit」命令中移除「--disableCommit」标记
==================================================
将提交信息写入 .git/COMMIT_EDITMSG 文件你可以使用 --amend 标志创建一个新的提交来替换当前分支的尖端。
codegpt commit --amend默认提交信息模板如下:
{{ .summarize_prefix }}: {{ .summarize_title }}
{{ .summarize_message }}使用 --template_string 参数更改模板格式:
codegpt commit --preview --template_string \
"[{{ .summarize_prefix }}]: {{ .summarize_title }}"使用 --template_file 参数更改模板文件格式:
codegpt commit --preview --template_file your_file_path向 git 提交信息模板添加自定义变量:
{{ .summarize_prefix }}: {{ .summarize_title }}
{{ .summarize_message }}
{{ if .JIRA_URL }}{{ .JIRA_URL }}{{ end }}使用 --template_vars 参数向 git 提交信息模板添加自定义变量:
codegpt commit --preview --template_file your_file_path --template_vars \
JIRA_URL=https://jira.example.com/ABC-123使用 --template_vars_file 参数从文件加载自定义变量:
codegpt commit --preview --template_file your_file_path --template_vars_file your_file_path参见 template_vars_file 格式如下:
JIRA_URL=https://jira.example.com/ABC-123你还可以使用 prepare-commit-msg hook 将 codegpt 与 Git 集成。这允许你正常使用 Git 并在提交前编辑提交信息。
你想在 Git 仓库中安装 hook:
codegpt hook install你想从 Git 仓库中移除 hook:
codegpt hook uninstall安装后暂存你的文件并提交:
git add <files...>
git commitcodegpt 将为你生成提交信息并将其传回 Git。Git 将使用配置的编辑器打开它供你审查/编辑。然后,要提交,请保存并关闭编辑器!
$ git commit
使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
================提交摘要====================
改进 OpenAI 工具的用户体验和文档
- 添加预编译二进制文件的下载链接
- 包括


