Git 操作工具
taskflow_git 提供完整的 Git 版本控制能力
📋 可用工具
| 工具名 | 功能 | 安全性 |
|---|---|---|
taskflow_git_status | 查看 Git 状态 | ✅ Low |
taskflow_git_diff | 查看差异 | ✅ Low |
taskflow_git_log | 查看提交历史 | ✅ Low |
taskflow_git_branch | 分支操作 | ✅ Medium |
taskflow_git_commit | 提交更改 | ✅ High |
taskflow_git_push | 推送更改 | ✅ High |
taskflow_git_pull | 拉取更改 | ✅ High |
taskflow_git_checkout | 切换分支 | ✅ Medium |
🔧 工具详解
status
查看 Git 状态。
参数:
json
{
"path": "string" // 仓库路径(可选,默认工作目录)
}示例:
Claude: 查看 Git 状态调用:
json
{
"name": "taskflow_git_status",
"arguments": {}
}返回:
json
{
"success": true,
"branch": "main",
"changes": [
{"file": "src/index.ts", "status": "modified"},
{"file": "src/utils.ts", "status": "new"}
],
"ahead": 0,
"behind": 2
}diff
查看差异。
参数:
json
{
"path": "string", // 文件路径(可选)
"cached": false // 是否查看暂存区(可选)
}示例:
Claude: 查看 src/index.ts 的差异调用:
json
{
"name": "taskflow_git_diff",
"arguments": {
"path": "src/index.ts"
}
}返回:
json
{
"success": true,
"diff": "@@ -1,5 +1,6 @@\n import { foo } from './foo';\n+import { bar } from './bar';\n ..."
}commit
提交更改。
参数:
json
{
"message": "string", // 提交信息(必填)
"files": ["string"], // 文件列表(可选,默认全部)
"all": false // 是否包含所有更改(可选)
}示例:
Claude: 提交更改,信息是 "feat: add new feature"调用:
json
{
"name": "taskflow_git_commit",
"arguments": {
"message": "feat: add new feature",
"all": true
}
}返回:
json
{
"success": true,
"commit": "abc1234",
"message": "feat: add new feature"
}push
推送更改。
参数:
json
{
"remote": "origin", // 远程名称(可选)
"branch": "main" // 分支名(可选)
}示例:
Claude: 推送到远程调用:
json
{
"name": "taskflow_git_push",
"arguments": {
"remote": "origin",
"branch": "main"
}
}返回:
json
{
"success": true,
"remote": "origin",
"branch": "main"
}🔒 安全机制
1. 命令链检测
Git 命令自动检测危险模式:
typescript
// 检测命令链
if (command.includes('&&') || command.includes('||')) {
throw new Error('命令链不被允许');
}2. 分支保护
默认保护以下分支:
yaml
protectedBranches:
- main
- master
- production3. 提交验证
提交前验证:
- ✅ 提交信息格式
- ✅ 文件类型
- ✅ 敏感文件检测
4. 远程验证
推送前验证远程:
typescript
// 验证远程 URL
const remote = await git.getRemoteUrl('origin');
if (!remote.startsWith('https://') && !remote.startsWith('git@')) {
throw new Error('无效的远程 URL');
}💡 使用技巧
技巧一:原子提交
Claude: 帮我提交 src/utils.ts 的更改Claude 会只提交指定文件,保持提交原子性。
技巧二:查看历史
Claude: 查看最近的 5 次提交Claude 会调用 git log -5 并返回摘要。
技巧三:分支管理
Claude: 创建并切换到新分支 feature/new-featureClaude 会依次执行:
bash
git checkout -b feature/new-feature🐛 常见问题
Q: 推送被拒绝
A: 远程有未同步的更改。尝试:
bash
git pull --rebase
git pushQ: 提交失败
A: 检查提交信息格式:
bash
# 使用规范的提交信息
git commit -m "feat: add new feature"
# 或
git commit -m "fix: resolve issue #123"Q: 分支保护
A: 无法直接推送到保护分支。尝试:
- 创建功能分支
- 提交到功能分支
- 创建 Pull Request
🔗 相关链接
Git 操作有问题随时问我