aicommit2 是一个自动生成 commit 信息的 CLI 工具,支持多家供应商平台的 AI 模型,我使用的是 DeepSeek 的模型,以此为示例演示使用。
安装:
aicommit2 工具依赖 Node.js ,支持的最低版本是V18,使用 node --version 检查 Node.js 版本。
直接安装即可:
npm install -g aicommit2
配置:
我这边 DeepSeek 用到的配置参考:
locale=chinese
[DEEPSEEK]
key=sk-***
model=deepseek-coder
temperature=1
includeBody=true
systemPromptPath=C:\Users\myname\.aicommit2.conf\prompt.txt
maxTokens=3000
配置项可以通过以下命令配置:
aicommit2 config set key=value
# 示例:
aicommit2 config set locale="chinese"
aicommit2 config set DEEPSEEK.model="deepseek-coder"
具体支持的配置和详细介绍可以参考项目说明:general-settings
这里说明一下,配置中的 locale 按照文档需要中文应该设置为 zh,但是经过实验,效果不太理想,看了下默认提示词后发现使用 chinese 更好。
配置完之后就可以使用 aicommit2 命令生成 commit 信息了:
git add .
aicommit2
# 输出:
_ _ _ ___
__ _(_)__ ___ _ __ _ __ (_) |_|_ )
/ _` | / _/ _ \ ' \| ' \| | _|/ /
\__,_|_\__\___/_|_|_|_|_|_|_|\__/___|
✔ Detected 1 staged file:
test.php
? Pick a commit message to use: (Use ↑↓ arrows to move)
❯ [DeepSeek] docs: 添加测试代码注释
docs: 添加测试代码注释
- 在测试代码前添加注释以说明其用途
✔ Changes analyzed
如果你配置了多个 AI 模型服务,则每个模型都会生成一个 commit 信息,只需要方向键选择一个,然后输入 yes 回车就使用选中的那条 commit 信息自动提交了,如果你想要对信息做调整,继续往下看。
基于 git hook 编辑 commit 信息
有时候我们需要做一些修改,通过添加 prepare_commit_msg 的 git hook 来实现对 commit 信息的编辑。
在项目目录下运行以下命令即可:
aicommit2 hook install
安装完这个勾子之后,使用普通的 git 命令提交即可,会自动触发 aicommit2 来生成 commit 信息:
git add .
git commit
hook 会自动触发 aicommit2 生成 commit 信息,完成后自动弹出编辑界面,操作与 vim 类似,编辑信息后保存即完成提交。
如果想要删除这个勾子,运行:
aicommit2 hook uninstall
自定义提示词
配置中的自定义提示词路径 systemPromptPath 参数支持自定义提示词文本文件路径。
prompt.txt 的内容是我正在使用的提示词,对官方提示词做了一些小修改,仅供参考,这里的大括号里面的支持的变量,具体说明看官方项目说明:
You are a helpful assistant specializing in writing clear and informative Git commit messages using the {type} style
Based on the given code changes or context, generate exactly 1 {type} Git commit message based on the following guidelines.
1. Message Language: {locale}
2. Format: follow the {type} Commits format:
<type>: <description in {locale}>
[optional body]
[optional footer(s)]
3. Types: use one of the following types:
- docs: Documentation only changes
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing tests or correcting existing tests
- build: Changes that affect the build system or external dependencies
- ci: Changes to CI configuration files, scripts
- chore: Other changes that don't modify src or test files
- revert: Reverts a previous commit
- feat: A new feature
- fix: A bug fix
4. Exclude anything unnecessary such as translation. Your entire response will be passed directly into git commit.
可以根据自己的需要来自定义提示词让它符合你的个性化需求,至此,就可以愉快的让 AI 帮你写 commit 信息了。
这个 aicommit2 还支持通过 Ollama 本地部署的模型,更多使用方法请参考项目说明,感谢作者 @tak-bro ,开源地址: