关于 git-workflow
Git Workflow 技能为团队协作提供分支策略、提交规范和 PR 流程指导。适合建立代码审查与发布流程的团队,也介绍钩子、分支保护和常见 Git 操作。
主要功能
- 01比较 trunk-based、GitHub Flow 和 GitFlow
- 02采用 Conventional Commits 规范
- 03配置 PR 模板与分支保护
- 04使用 Git hooks 检查提交
- 05通过标签和变更日志管理发布
适用场景
- 01为团队选择并建立分支策略
- 02统一提交消息并配置提交检查
- 03设置代码审查与发布流程
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill git-workflow安装后在 Claude Code 中输入/git-workflow确认已加载。
详细介绍
Git 工作流
为团队协作实施有效的分支策略和拉取请求工作流。
何时使用此技能
在以下情况下使用此技能:
- 建立团队 Git 工作流
- 实施分支策略
- 配置拉取请求流程
- 设置发布管理
- 改进代码审查实践
前置条件
- 已安装 Git
- 代码仓库托管平台(GitHub、GitLab、Bitbucket)
- 具备 Git 基础知识
分支策略
主干开发
最适用于:持续部署、小型团队、成熟的 CI/CD
main ─────●─────●─────●─────●─────●─────●─────●
│ │ │ │ │ │
└─● └─● └─● └─● └─● └─●
feature branches (short-lived)
# Create short-lived feature branch
git checkout main
git pull origin main
git checkout -b feature/add-login
# Work and commit frequently
git add .
git commit -m "feat: add login form"
# Keep branch updated
git fetch origin
git rebase origin/main
# Merge quickly (same day ideally)
git checkout main
git pull origin main
git merge feature/add-login
git push origin main
git branch -d feature/add-login
GitHub Flow
最适用于:持续交付、Web 应用
main ─────●─────●───────────●─────────────●─────●
│ ↑ ↑ ↑
└───●───●───┘ │ │
feature/login │ │
│ │
└───●───●───●───●───────┘ │
feature/dashboard │
│
└───●─────────────────────────┘
hotfix/security-patch
# Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-dashboard
# Push and create PR
git push -u origin feature/user-dashboard
# After review, merge via PR (squash recommended)
# Delete branch after merge
GitFlow
最适用于:按计划发布、带版本号的产品
main ────────●────────────────●──────────────●
↑ ↑ ↑
release ────────┼────●───●──────┼──────────────┼
│ │ │ │ │
develop ───●────●────┼───●──●───●───●───●───●──┼
│ │ │ │ │ │
feature ───┴─────────┘ │ │ │ │
│ │ │ │
hotfix ────────────────────┴───────┼───┼──────┘
│ │
feature ────────────────────────────┴───┘
# Initialize GitFlow
git flow init
# Start feature
git flow feature start user-auth
# Finish feature (merges to develop)
git flow feature finish user-auth
# Start release
git flow release start 1.0.0
# Finish release (merges to main and develop)
git flow release finish 1.0.0
# Hotfix
git flow hotfix start security-fix
git flow hotfix finish security-fix
提交约定
Conventional Commits
<type>(<scope>): <description>
[optional body]
[optional footer(s)]
类型:
feat:新功能fix:修复缺陷docs:文档style:格式调整refactor:代码重构test:添加测试chore:维护
示例:
git commit -m "feat(auth): add OAuth2 login support"
git commit -m "fix(api): handle null response from payment service"
git commit -m "docs: update API documentation for v2 endpoints"
git commit -m "refactor(db): optimize user query performance"
# Breaking change
git commit -m "feat(api)!: change response format for user endpoint
BREAKING CHANGE: The user endpoint now returns an object instead of array"
提交消息模板
# Create template file ~/.gitmessage
# Subject line (50 chars max)
# Body (72 chars per line max)
# - What changed
# - Why it changed
# - Any side effects
# Footer
# Fixes #123
# Co-authored-by: Name <email>
# Configure Git to use template
git config --global commit.template ~/.gitmessage
拉取请求工作流
PR 模板
## Description
Brief description of changes
## Type of Change
- [ ] Bug fix (non-breaking change)
- [ ] New feature (non-breaking change)
- [ ] Breaking change
- [ ] Documentation update
## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests added/updated
- [ ] Manual testing performed
## Checklist
- [ ] Code follows project style guidelines
- [ ] Self-review performed
- [ ] Documentation updated
- [ ] No new warnings introduced
## Related Issues
Closes #
## Screenshots (if applicable)
分支保护规则
# GitHub branch protection
branch_protection:
branch: main
required_pull_request_reviews:
required_approving_review_count: 1
dismiss_stale_reviews: true
require_code_owner_reviews: true
required_status_checks:
strict: true
contexts:
- "ci/tests"
- "ci/lint"
restrictions:
users: []
teams: ["maintainers"]
enforce_admins: true
required_linear_history: true
allow_force_pushes: false
allow_deletions: false
代码所有者
# .github/CODEOWNERS
# Default owners
* @team-leads
# Frontend code
/src/frontend/ @frontend-team
*.tsx @frontend-team
*.css @frontend-team
# Backend code
/src/api/ @backend-team
/src/services/ @backend-team
# Infrastructure
/terraform/ @platform-team
/k8s/ @platform-team
Dockerfile @platform-team
# Documentation
/docs/ @tech-writers
*.md @tech-writers
Git 钩子
提交前钩子
#!/bin/sh
# .git/hooks/pre-commit
# Run linting
npm run lint
if [ $? -ne 0 ]; then
echo "Linting failed. Fix errors before committing."
exit 1
fi
# Run tests
npm run test:unit
if [ $? -ne 0 ]; then
echo "Tests failed. Fix tests before committing."
exit 1
fi
# Check for debug statements
if grep -r "console.log\|debugger\|binding.pry" --include="*.js" --include="*.ts" --include="*.rb" src/; then
echo "Remove debug statements before committing."
exit 1
fi
使用 Husky
// package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"*.{js,ts,tsx}": ["eslint --fix", "prettier --write"],
"*.{css,scss}": ["prettier --write"]
}
}
Commitlint 配置
// commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'revert']
],
'subject-max-length': [2, 'always', 72],
'body-max-line-length': [2, 'always', 100]
}
};
发布工作流
使用标签自动发布
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tag
git push origin v1.0.0
# Create release from tag (GitHub CLI)
gh release create v1.0.0 \
--title "Release 1.0.0" \
--notes "Release notes here" \
--target main
生成变更日志
# Using conventional-changelog
npx conventional-changelog -p angular -i CHANGELOG.md -s
# Using git-cliff
git cliff -o CHANGELOG.md
常见 Git 操作
Rebase 与 Merge
# Rebase (clean history)
git checkout feature/my-feature
git rebase main
git push --force-with-lease
# Merge (preserve history)
git checkout main
git merge feature/my-feature
# Squash merge (single commit)
git merge --squash feature/my-feature
git commit -m "feat: add feature X"
Cherry Pick
# Apply specific commit to current branch
git cherry-pick abc123
# Cherry pick range
git cherry-pick abc123..def456
# Cherry pick without committing
git cherry-pick -n abc123
交互式 Rebase
# Clean up last 3 commits
git rebase -i HEAD~3
# In editor:
# pick abc123 First commit
# squash def456 Second commit
# reword ghi789 Third commit
常见问题
问题:合并冲突
问题:合并分支时发生冲突 解决方案:经常执行 rebase,与团队沟通,使用规模更小的 PR
问题:分支分歧
问题:本地分支远远落后于远程分支
解决方案:使用 git pull --rebase 或 git fetch && git rebase origin/main
问题:误将提交提交到了错误的分支
问题:提交到了 main,而不是功能分支
解决方案:使用 git reset HEAD~1,切换到正确的分支,然后重新提交
最佳实践
- 让分支的生命周期尽量短(< 1 周)
- 编写有意义的提交消息
- 始终使用 PR 模板
- 要求进行代码审查
- 保护 main/master 分支
- 使用 CI 自动运行检查
- 使用压缩合并保持历史记录整洁
- 合并后删除分支
相关技能
- semantic-versioning - 版本管理
- github-actions - CI/CD 自动化
- feature-flags - 功能管理
评论
支持 Markdown