关于 git-workflow
git-workflow 是面向开发者的 Git 实践指南,涵盖提交、分支、拉取请求、历史管理和协作。它按优先级组织规则,并提供示例与审查问题格式。
主要功能
- 01提交信息规范与示例
- 02分支命名和策略
- 03拉取请求实践
- 04历史管理与冲突处理
- 05协作与 Git Hooks 指南
适用场景
- 01编写清晰一致的提交信息
- 02规划分支及发布流程
- 03审查并拆分拉取请求
- 04整理历史并处理合并冲突
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ asyrafhussin/ agent-skills --skill git-workflow安装后在 Claude Code 中输入/git-workflow确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdmetadata.jsonREADME.mdrules/_sections.md_template.mdbranch-delete-merged.mdbranch-feature-workflow.mdbranch-main-protected.mdbranch-monorepo.mdbranch-naming-convention.mdbranch-release-strategy.mdbranch-short-lived.mdbranch-workflow-strategies.mdcollab-code-review.mdcollab-communication.mdcollab-conflict-resolution.mdcollab-gitignore.mdcommit-atomic-changes.mdcommit-body-context.mdcommit-breaking-changes.mdcommit-conventional-format.mdcommit-git-hooks.mdcommit-imperative-mood.mdcommit-meaningful-subject.mdcommit-references.mdhistory-clean-commits.mdhistory-no-force-push.mdhistory-rebase-vs-merge.mdhistory-tags-releases.mdhistory-worktree.md详细介绍
Git 工作流
Git 最佳实践、提交约定、分支策略和拉取请求工作流。维护整洁、实用的 Git 历史记录指南。
适用场景
以下情况请参考这些指南:
- 编写提交消息
- 创建分支
- 设置 Git 工作流
- 审查拉取请求
- 维护 Git 历史记录
按优先级划分的规则类别
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 提交消息 | 关键 | commit- |
| 2 | 分支策略 | 高 | branch- |
| 3 | 拉取请求 | 高 | pr- |
| 4 | 历史记录管理 | 中 | history- |
| 5 | 协作 | 中 | collab- |
快速参考
1. 提交消息(关键)
commit-conventional- 使用约定式提交commit-atomic- 原子提交(一个逻辑变更)commit-present-tense- 使用祈使式现在时commit-meaningful- 编写描述清晰、有意义的消息commit-body- 为复杂变更添加正文commit-references- 引用问题/工单commit-git-hooks- 使用 Husky + commitlint 强制执行规范
2. 分支策略(高)
branch-naming- 统一分支命名branch-feature- 功能分支工作流branch-main-protected- 保护 main 分支branch-short-lived- 保持分支生命周期较短branch-delete-merged- 删除已合并的分支branch-release- 发布分支策略branch-workflow-strategies- GitFlow、GitHub Flow 与 Trunk-Based 对比branch-monorepo- Monorepo Git 工作流
3. 拉取请求(高)
pr-small- 让 PR 保持小而聚焦pr-description- 编写清晰的说明pr-reviewers- 邀请合适的审查者pr-ci-pass- 确保 CI 通过pr-squash- 适时压缩提交pr-draft- 使用草稿 PR 处理进行中的工作并尽早获取反馈
4. 历史记录管理(中)
history-rebase- 变基与合并history-no-force-push- 避免强制推送到共享分支history-clean- 保持历史记录整洁history-tags- 使用标签标记发布版本history-worktree- 使用 git worktree 在多个分支上工作
5. 协作(中)
collab-code-review- 高效的代码审查collab-conflicts- 处理合并冲突collab-communication- 沟通变更collab-gitignore- .gitignore 最佳实践
核心指南
约定式提交
# Format
<type>(<scope>): <subject>
<body>
<footer>
类型
| 类型 | 描述 |
|---|---|
feat | 新功能 |
fix | 修复缺陷 |
docs | 仅修改文档 |
style | 格式调整,不修改代码 |
refactor | 修改代码,但不涉及功能或缺陷修复 |
perf | 性能优化 |
test | 添加或更新测试 |
chore | 维护、依赖项 |
ci | CI/CD 变更 |
build | 构建系统变更 |
revert | 撤销之前的提交 |
示例
# ✅ Good commit messages
feat(auth): add password reset functionality
fix(cart): resolve quantity update race condition
docs(readme): add installation instructions
refactor(api): extract validation into middleware
test(user): add unit tests for registration
chore(deps): update dependencies to latest versions
# With body and footer
feat(orders): implement order cancellation
Add ability for users to cancel pending orders within 24 hours.
Cancelled orders trigger refund process automatically.
Closes #123
BREAKING CHANGE: Order status enum now includes 'cancelled'
# ❌ Bad commit messages
fix bug
update
WIP
asdfasdf
changes
misc fixes
分支命名
# ✅ Good branch names
feature/user-authentication
feature/JIRA-123-password-reset
fix/cart-total-calculation
fix/issue-456-login-redirect
hotfix/security-patch
docs/api-documentation
refactor/database-queries
chore/update-dependencies
# ❌ Bad branch names
new-feature
john-branch
test
fix
temp
asdf
分支工作流
# Main branches
main # Production-ready code
develop # Integration branch (optional)
# Supporting branches
feature/* # New features
fix/* # Bug fixes
hotfix/* # Production hotfixes
release/* # Release preparation
# Workflow
git checkout main
git pull origin main
git checkout -b feature/user-profile
# Work on feature...
git add .
git commit -m "feat(profile): add profile page"
# Keep branch updated
git fetch origin
git rebase origin/main
# Push and create PR
git push -u origin feature/user-profile
原子提交
# ❌ One commit doing multiple things
git commit -m "Add login, fix header, update deps"
# ✅ Separate commits for each change
git commit -m "feat(auth): add login page"
git commit -m "fix(header): correct navigation alignment"
git commit -m "chore(deps): update React to v18.2"
频繁提交,定期推送
# ✅ Small, frequent commits
git commit -m "feat(cart): add product to cart"
git commit -m "feat(cart): display cart item count"
git commit -m "feat(cart): implement remove item"
git commit -m "test(cart): add unit tests"
# ❌ One massive commit
git commit -m "feat: implement entire shopping cart"
拉取请求最佳实践
PR 标题
# Format (like commit)
<type>(<scope>): <description>
# ✅ Good PR titles
feat(auth): implement OAuth2 login
fix(checkout): resolve payment processing error
docs(api): add endpoint documentation
# ❌ Bad PR titles
Update
Fix stuff
WIP
PR 描述模板
## Summary
Brief description of what this PR does.
## Changes
- Added user authentication endpoints
- Implemented JWT token generation
- Added password hashing with bcrypt
## Testing
- [ ] Unit tests pass
- [ ] Integration tests pass
- [ ] Manual testing completed
## Screenshots (if UI changes)
[Add screenshots here]
## Related Issues
Closes #123
Related to #456
## Checklist
- [ ] Code follows project conventions
- [ ] Self-review completed
- [ ] Tests added/updated
- [ ] Documentation updated
保持 PR 精简
# ✅ Focused PRs
PR #1: Add user model and migrations
PR #2: Implement user registration endpoint
PR #3: Add email verification
PR #4: Implement login/logout
# ❌ Large unfocused PR
PR #1: Add entire user authentication system
(2000+ lines, 50+ files)
变基与合并
# ✅ Rebase for feature branches (clean history)
git checkout feature/my-feature
git fetch origin
git rebase origin/main
# Resolve any conflicts
git push --force-with-lease # Only your branch!
# ✅ Merge for integrating to main (preserve context)
git checkout main
git merge --no-ff feature/my-feature
# Creates merge commit, preserves branch history
# ❌ Never force push to shared branches
git push --force origin main # NEVER DO THIS
交互式变基(清理)
# Before pushing, clean up commits
git rebase -i HEAD~5
# In editor:
pick abc123 feat: add login page
squash def456 fix typo
squash ghi789 more fixes
pick jkl012 feat: add logout
# Result: clean history with meaningful commits
处理冲突
# During rebase
git rebase origin/main
# CONFLICT in file.ts
# 1. Open conflicted files
# 2. Resolve conflicts (remove markers)
# 3. Stage resolved files
git add file.ts
# 4. Continue rebase
git rebase --continue
# Or abort if needed
git rebase --abort
Git 钩子
# .husky/pre-commit
#!/bin/sh
npm run lint
npm run test:unit
# .husky/commit-msg
#!/bin/sh
npx commitlint --edit $1
# commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
};
标记发布版本
# Semantic versioning tags
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
# List tags
git tag -l "v1.*"
# Tag format
v1.0.0 # Major.Minor.Patch
v1.0.0-beta.1 # Pre-release
v1.0.0-rc.1 # Release candidate
.gitignore 最佳实践
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
.next/
# Environment files
.env
.env.local
.env.*.local
# IDE
.idea/
.vscode/
*.swp
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Test coverage
coverage/
# Cache
.cache/
*.cache
实用 Git 命令
# View commit history
git log --oneline --graph --all
# See what changed
git diff --staged
git diff HEAD~1
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Discard local changes
git checkout -- file.ts
git restore file.ts # Git 2.23+
# Stash changes
git stash
git stash pop
git stash list
# Cherry-pick commit
git cherry-pick abc123
# Find who changed a line
git blame file.ts
# Search commits
git log --grep="fix"
git log -S "functionName"
输出格式
审查 Git 实践时,请输出以下发现:
[category] Description of issue or suggestion
示例:
[commit] Use imperative mood: "Add feature" not "Added feature"
[branch] Branch name 'test' should follow pattern: feature/description
[pr] PR is too large (50+ files), consider splitting
使用方法
阅读各个规则文件,了解详细说明:
rules/commit-conventional-format.md
rules/branch-naming-convention.md
rules/pr-small-focused.md
参考资料
- Git 官方文档 - 全面的 Git 文档
- Pro Git Book - 完整的 Pro Git 书籍
- Conventional Commits - 提交消息规范
- GitHub Flow - 轻量级工作流
- 如何编写 Git 提交消息 - 提交消息指南
- Google 代码审查实践 - 代码审查最佳实践
知名项目示例
向采用出色 Git 实践的项目学习:
- Linux Kernel - 详细的提交消息和补丁工作流
- React - Conventional Commits 和全面的 PR 审查
- Vue.js - 清晰的提交历史和完善的 PR 模板
- TypeScript - 结构化的分支管理和清晰的发布流程
- Next.js - Conventional Commits 和自动化发布
配置示例
Commitlint
// commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore', 'ci', 'build', 'revert']
],
'subject-max-length': [2, 'always', 72],
'body-max-line-length': [2, 'always', 100]
}
};
Husky Git Hooks
# .husky/commit-msg
#!/bin/sh
npx commitlint --edit $1
# .husky/pre-commit
#!/bin/sh
npm run lint
npm test
GitHub PR 模板
# .github/PULL_REQUEST_TEMPLATE.md
## Summary
Brief description of changes
## Changes
- List key changes
- One per line
## Testing
- [ ] Unit tests pass
- [ ] Integration tests pass
- [ ] Manual testing completed
## Related
Closes #issue-number
元数据
技能版本: 1.2.0 最后更新: 2026-03-08 规则总数: 31 类别: 5(提交消息、分支策略、拉取请求、历史管理、协作)
兼容对象:
- Git 2.0+
- GitHub、GitLab、Bitbucket、Azure DevOps
推荐工具:
- Git - 版本控制系统
- GitHub CLI - GitHub 命令行工具
- Commitlint - 提交消息检查工具
- Husky - Git Hooks
- Semantic Release - 自动化版本管理
许可证
MIT 许可证。本技能按原样提供,用于教育和开发目的。
评论
支持 Markdown