LazyGit + Codex 自动生成中文 Commit Message 完整配置指南
适用环境:
txt
LazyGit 0.61.1
Codex CLI 0.130.0
macOS
Ghostty/iTerm2/Terminal
目标:
txt
a 暂存全部代码
C AI生成中文提交说明
c 打开Commit输入框
⌘V 粘贴提交说明
Enter 提交
P 推送
效果:
txt
feat(video): 新增视频全屏播放功能
fix(login): 修复登录状态失效问题
refactor(player): 重构播放器状态管理逻辑
一、创建 AI Commit 脚本
创建目录:
bash
mkdir -p ~/.scripts
创建脚本:
bash
vim ~/.scripts/ai-commit-codex.sh
内容如下:
bash
#!/bin/bash
OUT="/tmp/ai-commit-msg.txt"
ERR="/tmp/ai-commit-error.log"
DIFF=$(git diff --cached)
if [ -z "$DIFF" ]; then
echo "没有已暂存的代码,请先 Stage 文件"
exit 1
fi
printf "%s" "$DIFF" | codex exec -o "$OUT" "
请根据当前 Git 暂存区 diff 生成一条提交说明。
要求:
- 使用 Conventional Commit 格式
- type 使用英文
- scope 使用英文
- 描述使用简体中文
- 只输出一行
- 不要 Markdown
- 不要解释
- 不要代码块
格式:
type(scope): 中文描述
type 可选:
feat
fix
refactor
perf
style
docs
chore
test
build
ci
" 2>"$ERR"
MSG=$(cat "$OUT" | grep -v '^$' | tail -n 1)
if [ -z "$MSG" ]; then
echo "生成失败,请查看:$ERR"
exit 1
fi
echo "$MSG" | pbcopy
echo ""
echo "==============================="
echo "已复制到剪贴板"
echo "$MSG"
echo "==============================="
二、赋予执行权限
bash
chmod +x ~/.scripts/ai-commit-codex.sh
说明:
txt
chmod = 修改权限
+x = 增加执行权限
验证:
bash
ls -l ~/.scripts/ai-commit-codex.sh
看到:
txt
-rwxr-xr-x
表示脚本可执行。
三、单独测试脚本
确保有代码修改:
bash
git add .
执行:
bash
~/.scripts/ai-commit-codex.sh
输出:
txt
===============================
已复制到剪贴板
feat(video): 新增视频全屏播放功能
===============================
验证剪贴板:
bash
pbpaste
输出:
txt
feat(video): 新增视频全屏播放功能
说明脚本正常。
四、配置 LazyGit 自定义命令
编辑配置:
bash
vim ~/Library/Application\ Support/lazygit/config.yml
加入:
yaml
customCommands:
- key: "C"
description: "AI Commit Message by Codex"
context: "files"
command: "/Users/liuchunqiu3/.scripts/ai-commit-codex.sh"
output: log
注意:
yaml
output: log
作用:
txt
执行脚本
↓
结果显示在 LazyGit Log 面板
↓
不离开 LazyGit
↓
自动返回当前界面
不要使用:
yaml
subprocess: true
否则会跳到终端。
五、重启 LazyGit
关闭:
txt
q
重新启动:
bash
lazygit
六、使用流程
进入项目:
bash
cd 项目目录
lazygit
1. 暂存代码
txt
a
功能:
txt
Stage All
暂存全部修改
相当于:
bash
git add .
2. AI生成提交说明
txt
C
功能:
txt
AI Commit Message by Codex
执行:
bash
~/.scripts/ai-commit-codex.sh
输出:
txt
已复制到剪贴板
feat(video): 新增视频全屏播放功能
并自动复制到系统剪贴板。
3. 打开提交框
txt
c
功能:
txt
Commit
创建提交
出现:
txt
Commit message:
输入框。
4. 粘贴提交说明
Mac:
txt
⌘V
得到:
txt
feat(video): 新增视频全屏播放功能
5. 提交
txt
Enter
完成:
txt
Commit Success
6. 推送
txt
P
功能:
txt
Push
推送到远程仓库
相当于:
bash
git push
七、完整操作顺序
txt
a
↓
C
↓
c
↓
⌘V
↓
Enter
↓
P
八、常用快捷键对照表
文件 Files
| 按键 | 英文 | 中文 |
|---|---|---|
| a | Stage All | 暂存全部 |
| Space | Stage | 暂存当前文件 |
| Enter | View Diff | 查看修改 |
| e | Edit | 编辑文件 |
| d | Discard | 丢弃修改 |
提交 Commit
| 按键 | 英文 | 中文 |
|---|---|---|
| C | AI Commit Message by Codex | AI生成中文提交说明 |
| c | Commit | 提交代码 |
| A | Amend Commit | 修改最近一次提交 |
远程仓库 Remote
| 按键 | 英文 | 中文 |
|---|---|---|
| p | Pull | 拉取 |
| P | Push | 推送 |
| f | Fetch | 获取远程更新 |
分支 Branch
| 按键 | 英文 | 中文 |
|---|---|---|
| 3 | Branches | 分支列表 |
| Space | Checkout | 切换分支 |
| M | Merge | 合并分支 |
| n | New Branch | 新建分支 |
| d | Delete Branch | 删除分支 |
其他
| 按键 | 英文 | 中文 |
|---|---|---|
| ? | Help | 查看帮助 |
| q | Quit | 退出 |
| Tab | Next Panel | 切换面板 |
最终推荐工作流
txt
修改代码
↓
a Stage All
↓
C AI生成中文Commit
↓
c Commit
↓
⌘V
↓
Enter
↓
P Push
这是目前你环境(LazyGit 0.61.1 + Codex 0.130.0)下最顺手、最稳定的方案。