ai-commit

command module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2025 License: GPL-3.0 Imports: 3 Imported by: 0

README

AI Commit Tool

一个使用 AI 根据你的代码暂存更改 (staged changes) 自动生成 Conventional Commits 规范的 commit message 的 CLI 工具。


✨ 功能特性

  • AI 驱动: 利用大语言模型 (LLM) 的能力理解代码变更,并生成高质量的 commit message。
  • 规范化: 自动遵循 Conventional Commits 规范,让你的 git log 清晰可读。
  • 多服务支持: 支持 OpenAI, Google Gemini, OpenRouter 等多家 AI 服务。
  • 两种工作流:
    • 交互式 (默认): 在 git commit 时自动生成消息草稿,并在你的编辑器中打开,供你审查和修改。
    • 静默式: 通过 ai-commit commit 命令直接生成并提交,无需人工干预。
  • 高度可配置: 支持自定义 prompt 模板、网络代理等。
  • 易于安装: 单个二进制文件,通过 Git 钩子与你的工作流无缝集成。

🚀 安装

1. 获取工具

你可以通过以下方式安装 ai-commit

a) 从源码构建 (需要 Go 环境):

go install github.com/AnTengye/ai-commit@latest

或者克隆本仓库后手动构建:

git clone https://github.com/AnTengye/ai-commit
cd ai-commit
go build
# 你可以把生成的 ai-commit 二进制文件移动到你的 PATH 路径下

b) 从 Releases 下载:

前往 Releases 页面 下载适合你操作系统的最新版本一个使用 AI 自动生成 commit 信息草稿的 CLI 工具。

2. 配置

工具需要一个 config.yaml 配置文件。你可以从 config.example.yaml 复制一份并修改。

config.yaml 放置在以下任意位置:

  • 项目级: 你当前 git 项目的根目录。
  • 全局: 用户主目录下的 .ai-commit 文件夹 (例如 ~/.ai-commit/config.yaml)。

一个最小化的配置示例:

# config.yaml

# 选择你的 AI 服务, e.g., "openai"
ai_provider: "openai"

# 配置你选择的 AI 服务
openai:
  # 强烈建议使用环境变量来设置 API Key
  # 环境变量: AICOMMIT_OPENAI_API_KEY 或 OPENAI_API_KEY
  api_key: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  model: "gpt-4o-mini"

详细的配置选项请参考 config.example.yaml 文件。

3. 安装 Git 钩子

进入你的 git 项目目录,运行以下命令来安装 prepare-commit-msg 钩子:

ai-commit install-hook

它会在 .git/hooks/ 目录下创建一个 prepare-commit-msg 文件。现在,当你运行 git commit 时,工具就会自动工作了。

💡 使用方式

安装和配置完成后,你有两种方式来使用此工具:

1. 交互式流程 (推荐)

这是默认的工作方式,通过 Git 钩子自动触发。

  1. 修改你的代码。
  2. 使用 git add . 暂存你的更改。
  3. 运行 git commit
  4. 工具会自动运行,AI 会根据你的更改生成 commit message。
  5. 你的默认 Git 编辑器会自动打开,并包含 AI 生成的消息草稿。
  6. 你可以检查、修改或直接保存并关闭编辑器来完成提交。
2. 静默/独立命令流程

如果你偏爱更自动化的流程,或者想在不安装钩子的情况下使用,可以使用 commit 子命令。

  1. 修改你的代码。
  2. 使用 git add . 暂存你的更改。
  3. 运行 ai-commit commit

这个命令的行为取决于你的配置:

  • 如果 silent: false (默认): 工具会生成 commit message 并将其打印到控制台。它不会自动提交,你可以复制这个 message 手动使用。
  • 如果 silent: true: 工具会生成 message 并直接为你执行 git commit,全程无需交互。

⚙️ 命令参考

  • ai-commit: 不带参数运行时,默认执行 commit 子命令。
  • ai-commit commit: 生成 commit message。根据 silent 配置决定是直接提交还是打印到控制台。
  • ai-commit install-hook: 在当前 git 仓库中安装 prepare-commit-msg 钩子。

📄 许可证

本项目基于 GPL License 发布。

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL