Skip to content

Git命令 (/git)

智能Git操作命令,支持智能提交消息生成、分支管理和工作流优化。

核心功能

  • 智能提交: 自动生成规范的提交消息
  • 分支管理: 应用分支命名约定和策略
  • 工作流优化: 集成Git最佳实践
  • 冲突处理: 智能合并冲突解决
  • 状态分析: 详细的仓库状态分析

使用语法

bash
/git [操作] [参数] [选项]

主要操作

  • status - 查看仓库状态
  • add - 暂存文件更改
  • commit - 提交更改
  • push - 推送到远程仓库
  • pull - 从远程仓库拉取
  • branch - 分支管理
  • merge - 合并分支

智能选项

  • --smart-commit - 生成智能提交消息
  • --branch-strategy - 应用分支命名约定
  • --interactive - 交互模式
  • --safe - 安全模式(确认危险操作)

智能提交消息

自动生成规范

bash
# 智能分析代码变更,生成规范提交消息
/git commit --smart-commit

# 输出示例:
# feat(auth): add JWT token validation
# 
# - Implement token expiration check
# - Add refresh token mechanism  
# - Update security middleware

提交消息格式

<type>(<scope>): <description>

<body>

<footer>

类型定义

  • feat: 新功能
  • fix: 错误修复
  • docs: 文档更新
  • style: 代码格式化
  • refactor: 重构
  • test: 测试相关
  • chore: 构建和辅助工具

🌿 分支管理策略

智能分支命名

bash
# 创建功能分支
/git branch feature/user-authentication --branch-strategy

# 创建修复分支
/git branch hotfix/login-error --branch-strategy

# 创建发布分支
/git branch release/v1.2.0 --branch-strategy

分支命名约定

  • feature/[功能名] - 新功能开发
  • bugfix/[问题描述] - 错误修复
  • hotfix/[紧急修复] - 紧急修复
  • release/[版本号] - 发布准备
  • experiment/[实验名] - 实验性功能

常用操作示例

日常开发流程

bash
# 查看当前状态
/git status

# 暂存所有更改
/git add . --interactive

# 智能提交
/git commit --smart-commit

# 推送到远程
/git push --safe

功能开发流程

bash
# 创建功能分支
/git branch feature/shopping-cart --branch-strategy

# 切换到功能分支
/git checkout feature/shopping-cart

# 开发完成后提交
/git commit --smart-commit

# 合并到主分支
/git merge main --interactive

发布流程

bash
# 创建发布分支
/git branch release/v2.1.0 --branch-strategy

# 标记发布版本
/git tag v2.1.0 --smart-commit

# 推送发布
/git push origin release/v2.1.0 --safe

高级功能

交互模式

bash
# 交互式暂存
/git add --interactive

# 交互式合并
/git merge --interactive

# 交互式变基
/git rebase --interactive

冲突解决

bash
# 智能合并冲突分析
/git merge main --conflict-analysis

# 提供解决建议和工具推荐
# 自动检测冲突类型和复杂度
# 生成解决步骤指导

历史分析

bash
# 分析提交历史
/git log --analysis

# 生成变更报告
/git diff --report

# 代码贡献统计
/git stats --contributors

安全特性

危险操作保护

bash
# 强制推送前确认
/git push --force --safe

# 重置前备份
/git reset --hard --safe

# 删除分支前确认
/git branch -d feature/old --safe

敏感信息检查

  • 提交前扫描敏感信息
  • API密钥和密码检测
  • 大文件和不当内容警告
  • .gitignore规则建议

工作流集成

CI/CD集成

bash
# 触发构建的提交
/git commit --trigger-ci

# 跳过CI的提交
/git commit --skip-ci

# 发布标签
/git tag --release

团队协作

bash
# 多人协作状态检查
/git status --team

# 分支同步建议
/git sync --recommendations

# 合并策略优化
/git merge --team-strategy

自定义配置

提交模板配置

json
{
  "git_preferences": {
    "commit_template": "conventional",
    "branch_strategy": "gitflow",
    "auto_push": false,
    "safe_mode": true,
    "smart_messages": true
  }
}

工作流模板

  • GitFlow: 标准Git工作流
  • GitHub Flow: 简化分支策略
  • GitLab Flow: 环境分支策略
  • Custom: 自定义工作流规则

最佳实践

1. 规范的提交习惯

bash
# 小而频繁的提交
/git commit --smart-commit --atomic

# 有意义的提交消息
/git commit --message-quality-check

2. 分支管理

bash
# 及时清理无用分支
/git branch --cleanup

# 保持分支同步
/git pull --rebase

3. 代码审查准备

bash
# 推送前检查
/git push --pre-push-checks

# 生成变更摘要
/git diff --summary

工具集成

Claude Code工具

  • Bash: Git命令执行
  • Read: 仓库文件分析
  • Glob: 文件模式匹配
  • TodoWrite: 操作进度跟踪
  • Edit: 冲突解决辅助

外部工具

  • GitHub CLI: GitHub集成
  • GitLab CLI: GitLab集成
  • Git GUI工具: SourceTree、Fork等
  • IDE集成: VS Code、JetBrains等

Git命令 - 让版本控制更智能、更规范 🌿

Claude Code 使用指南