本文档记录了如何为 Gemini CLI 创建一个名为 git-to-log 的自定义技能。该技能可以自动从多个 Git 仓库提取提交记录,并将其整理为结构化的日报或周报内容。
1. 目标与需求
核心目标: 自动化提取 Git 提交记录 -> 智能分类 -> 生成 Markdown 日志。
具体需求:
- 多仓库支持:能同时读取
server,proto,configs等关联仓库的提交。 - 灵活参数:支持指定日期范围(如“今天”、“上周”)和作者过滤。
- 智能分类:根据 Commit Message 的前缀(
feat,fix,docs等)自动归类为“业务”、“工具”、“修复”等。 - 无缝集成:通过自然语言指令(如“生成今天的日报”)触发。
2. 开发环境准备
确保已安装 Gemini CLI,并激活了 skill-creator 技能(可选,但推荐用于辅助生成):
# 激活技能创建向导
activate_skill skill-creator
3. 初始化技能
我们首先在一个临时目录或工作区中初始化技能结构。
命令:
# 创建目录
mkdir -p .gemini_temp/git-to-log
# 使用 skill-creator 的脚本初始化
node <path-to-skill-creator>/scripts/init_skill.cjs git-to-log --path .gemini_temp
生成的目录结构:
git-to-log/
├── SKILL.md # 核心配置文件(元数据 + 指令)
├── scripts/ # 可执行脚本目录
│ └── fetch_git_logs.cjs # (我们需要新建的核心脚本)
├── references/ # 参考文档(可选)
└── assets/ # 静态资源(可选)
4. 编写核心代码
4.1 编写日志提取脚本 (scripts/fetch_git_logs.cjs)
这个脚本负责调用系统 git 命令,并返回标准化的 JSON 数据供 LLM 处理。
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
// 解析参数
const args = process.argv.slice(2);
const getArg = (name) => {
const index = args.indexOf(name);
return index !== -1 ? args[index + 1] : null;
};
const since = getArg('--since') || '1 day ago';
const until = getArg('--until');
const author = getArg('--author');
const reposArg = getArg('--repos');
const repos = reposArg ? reposArg.split(',') : ['.'];
const allLogs = [];
repos.forEach(repoPath => {
const resolvedPath = path.resolve(process.cwd(), repoPath);
if (!fs.existsSync(path.join(resolvedPath, '.git'))) {
return;
}
const repoName = path.basename(resolvedPath);
// 构造 git log 命令
let cmd = `git -C "${resolvedPath}" log --since="${since}" --pretty=format:"%h|%ad|%an|%s" --date=short`;
if (until) cmd += ` --until="${until}"`;
if (author) cmd += ` --author="${author}"`;
try {
const output = execSync(cmd, { encoding: 'utf8' });
if (!output) return;
output.split('\n').forEach(line => {
const [hash, date, commitAuthor, message] = line.split('|');
allLogs.push({
repo: repoName,
hash,
date,
author: commitAuthor,
message
});
});
} catch (error) {
console.error(`Error fetching logs from ${repoName}:`, error.message);
}
});
// 按时间倒序排列
allLogs.sort((a, b) => new Date(b.date) - new Date(a.date));
// 输出 JSON
console.log(JSON.stringify(allLogs, null, 2));
4.2 配置技能定义 (SKILL.md)
这是 Gemini 理解和使用技能的“说明书”。我们使用中文编写以提高可维护性。
---
name: git-to-log
description: 从多个 Git 仓库提取提交记录并格式化为日报或周报。当用户要求“生成日志”、“更新日报”、“汇总 Git 提交”或“创建周报总结”时使用。
---
# Git 日志生成技能 (Git to Log)
此技能用于自动化提取多个 Git 仓库的提交历史,并将其整理为结构化的 Markdown 工作日志。
## 工作流程
1. **确定参数**:
* **日期范围**: 解析用户指令中的时间(如“今天”、“上周”)。默认为 "1 day ago"。
* **仓库列表**: 默认检查当前目录 (`.`) 及兄弟目录(如 `../proto`, `../configs`)。
* **作者**: 默认为当前 Git 用户,可指定过滤。
2. **获取日志**:
* 调用脚本: `node <技能路径>/scripts/fetch_git_logs.cjs ...`
3. **处理与分类**:
* **过滤**: 忽略 Merge commits。
* **分类**:
* `feat(...)` -> **业务** / **工具**
* `fix(...)` -> **修复**
* `docs(...)` -> **文档**
* `chore/build/ci` -> **部署** / **框架**
4. **执行操作**:
* **展示**: 直接打印结果。
* **更新文件**: 读取指定日志文件(如 `2026-01.md`),插入内容后保存。
5. 打包与安装
为了方便分发和管理,我们需要将技能打包为 .skill 文件。
步骤:
-
清理冗余文件:删除初始化时生成的
example_script.cjs等。 -
打包:
node <path-to-skill-creator>/scripts/package_skill.cjs .gemini_temp/git-to-log .gemini_temp这将生成
.gemini_temp/git-to-log.skill文件。 -
安装到用户全局范围 (User Scope): 推荐安装到 User Scope,这样技能在任何项目目录下都可用,且不会污染特定项目的 git 仓库。
skills install .gemini_temp/git-to-log.skill --scope user ```
-
清理临时文件: 安装完成后,
.gemini_temp目录可以删除。 -
重载技能: 在 Gemini CLI 交互界面运行:
/skills reload
6. 使用 Git 管理个人技能
为了防止技能丢失并方便多机同步,建议将 ~/.gemini/skills 目录纳入 Git 管理。
cd ~/.gemini/skills
git init
echo ".DS_Store\n*.skill" > .gitignore
git add .
git commit -m "Initial commit: Add git-to-log skill"
# (可选) 推送到远程仓库
# git remote add origin git@github.com:username/my-gemini-skills.git
# git push -u origin main
这样,当你需要修改技能逻辑时:
- 直接修改
~/.gemini/skills/git-to-log/SKILL.md。 - 运行
/skills reload测试。 - 测试通过后
git commit提交代码。
7. 使用效果
用户: "生成今天的日志"
Gemini:
- 自动执行
fetch_git_logs.cjs获取今天server,proto,configs的提交。 - 识别到提交
feat(task): Add new feature,分类为“业务”。 - 生成如下 Markdown 并展示:
## 2026-02-02 周一
- 业务: Add new feature
- 工具: Fix build script