git-workflow-strategy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Workflow Strategy
Git工作流策略
Overview
概述
Establish efficient Git workflows that support team collaboration, code quality, and deployment readiness through structured branching strategies and merge patterns.
通过结构化的分支策略和合并模式,建立支持团队协作、代码质量和部署就绪的高效Git工作流。
When to Use
适用场景
- Team collaboration setup
- Release management
- Feature development coordination
- Hotfix procedures
- Code review processes
- CI/CD integration planning
- 团队协作设置
- 发布管理
- 功能开发协调
- 紧急修复流程
- 代码评审流程
- CI/CD集成规划
Implementation Examples
实现示例
1. GitFlow Workflow Setup
1. GitFlow工作流设置
bash
undefinedbash
undefinedInitialize GitFlow
Initialize GitFlow
git flow init -d
git flow init -d
Start a feature
Start a feature
git flow feature start new-feature
git flow feature start new-feature
Work on feature
Work on feature
git add .
git commit -m "feat: implement new feature"
git flow feature finish new-feature
git add .
git commit -m "feat: implement new feature"
git flow feature finish new-feature
Start a release
Start a release
git flow release start 1.0.0
git flow release start 1.0.0
Update version numbers, changelog
Update version numbers, changelog
git add .
git commit -m "chore: bump version to 1.0.0"
git flow release finish 1.0.0
git add .
git commit -m "chore: bump version to 1.0.0"
git flow release finish 1.0.0
Create hotfix
Create hotfix
git flow hotfix start 1.0.1
git flow hotfix start 1.0.1
Fix critical bug
Fix critical bug
git add .
git commit -m "fix: critical bug in production"
git flow hotfix finish 1.0.1
undefinedgit add .
git commit -m "fix: critical bug in production"
git flow hotfix finish 1.0.1
undefined2. GitHub Flow Workflow
2. GitHub Flow工作流
bash
undefinedbash
undefinedClone and setup
Clone and setup
git clone https://github.com/org/repo.git
cd repo
git clone https://github.com/org/repo.git
cd repo
Create feature branch from main
Create feature branch from main
git checkout -b feature/add-auth-service
git add .
git commit -m "feat: add authentication service"
git push origin feature/add-auth-service
git checkout -b feature/add-auth-service
git add .
git commit -m "feat: add authentication service"
git push origin feature/add-auth-service
Push changes, create PR, request reviews
Push changes, create PR, request reviews
After approval and CI passes, merge to main
After approval and CI passes, merge to main
git checkout main
git pull origin main
git merge feature/add-auth-service
git push origin main
git checkout main
git pull origin main
git merge feature/add-auth-service
git push origin main
Deploy and cleanup
Deploy and cleanup
git branch -d feature/add-auth-service
git push origin -d feature/add-auth-service
undefinedgit branch -d feature/add-auth-service
git push origin -d feature/add-auth-service
undefined3. Trunk-Based Development
3. Trunk-Based Development
bash
undefinedbash
undefinedCreate short-lived feature branch
Create short-lived feature branch
git checkout -b feature/toggle-feature
git checkout -b feature/toggle-feature
Keep commits small and atomic
Keep commits small and atomic
git add specific_file.js
git commit -m "feat: add feature flag configuration"
git add specific_file.js
git commit -m "feat: add feature flag configuration"
Rebase on main frequently
Rebase on main frequently
git fetch origin
git rebase origin/main
git fetch origin
git rebase origin/main
Create PR with small changeset
Create PR with small changeset
git push origin feature/toggle-feature
git push origin feature/toggle-feature
After PR merge, delete branch
After PR merge, delete branch
git checkout main
git pull origin main
git branch -d feature/toggle-feature
undefinedgit checkout main
git pull origin main
git branch -d feature/toggle-feature
undefined4. Git Configuration for Workflows
4. Git工作流配置
bash
undefinedbash
undefinedConfigure user
Configure user
git config --global user.name "Developer Name"
git config --global user.email "dev@example.com"
git config --global user.name "Developer Name"
git config --global user.email "dev@example.com"
Set default branch
Set default branch
git config --global init.defaultBranch main
git config --global init.defaultBranch main
Configure merge strategy
Configure merge strategy
git config --global pull.ff only
git config --global merge.ff false
git config --global pull.ff only
git config --global merge.ff false
Enable rerere (reuse recorded resolution)
Enable rerere (reuse recorded resolution)
git config --global rerere.enabled true
git config --global rerere.enabled true
Configure commit message format
Configure commit message format
git config --global commit.template ~/.gitmessage
git config --global commit.template ~/.gitmessage
Setup branch protection rules
Setup branch protection rules
git config --global branch.main.rebase true
git config --global branch.develop.rebase true
undefinedgit config --global branch.main.rebase true
git config --global branch.develop.rebase true
undefined5. Branch Naming Conventions
5. 分支命名规范
bash
undefinedbash
undefinedFeature branches
Feature branches
git checkout -b feature/user-authentication
git checkout -b feature/JIRA-123-payment-integration
git checkout -b feature/user-authentication
git checkout -b feature/JIRA-123-payment-integration
Bug fix branches
Bug fix branches
git checkout -b bugfix/JIRA-456-login-timeout
git checkout -b fix/null-pointer-exception
git checkout -b bugfix/JIRA-456-login-timeout
git checkout -b fix/null-pointer-exception
Release branches
Release branches
git checkout -b release/v2.1.0
git checkout -b release/2024-Q1
git checkout -b release/v2.1.0
git checkout -b release/2024-Q1
Hotfix branches
Hotfix branches
git checkout -b hotfix/critical-security-patch
git checkout -b hotfix/v2.0.1
git checkout -b hotfix/critical-security-patch
git checkout -b hotfix/v2.0.1
Chore branches
Chore branches
git checkout -b chore/update-dependencies
git checkout -b chore/refactor-auth-module
undefinedgit checkout -b chore/update-dependencies
git checkout -b chore/refactor-auth-module
undefined6. Merge Strategy Script
6. 合并策略脚本
bash
#!/bin/bashbash
#!/bin/bashmerge-with-strategy.sh
merge-with-strategy.sh
BRANCH=$1
STRATEGY=${2:-"squash"}
if [ -z "$BRANCH" ]; then
echo "Usage: ./merge-with-strategy.sh <branch> [squash|rebase|merge]"
exit 1
fi
BRANCH=$1
STRATEGY=${2:-"squash"}
if [ -z "$BRANCH" ]; then
echo "Usage: ./merge-with-strategy.sh <branch> [squash|rebase|merge]"
exit 1
fi
Update main
Update main
git checkout main
git pull origin main
case "$STRATEGY" in
squash)
git merge --squash origin/$BRANCH
git commit -m "Merge $BRANCH"
;;
rebase)
git rebase origin/$BRANCH
;;
merge)
git merge --no-ff origin/$BRANCH
;;
*)
echo "Unknown strategy: $STRATEGY"
exit 1
;;
esac
git push origin main
git push origin -d $BRANCH
undefinedgit checkout main
git pull origin main
case "$STRATEGY" in
squash)
git merge --squash origin/$BRANCH
git commit -m "Merge $BRANCH"
;;
rebase)
git rebase origin/$BRANCH
;;
merge)
git merge --no-ff origin/$BRANCH
;;
*)
echo "Unknown strategy: $STRATEGY"
exit 1
;;
esac
git push origin main
git push origin -d $BRANCH
undefined7. Collaborative Workflow with Code Review
7. 带代码评审的协作工作流
bash
undefinedbash
undefinedDeveloper creates feature
Developer creates feature
git checkout -b feature/search-optimization
git checkout -b feature/search-optimization
Make changes
Make changes
git add .
git commit -m "perf: optimize search algorithm"
git push origin feature/search-optimization
git add .
git commit -m "perf: optimize search algorithm"
git push origin feature/search-optimization
Create pull request with detailed description
Create pull request with detailed description
Reviewer reviews and suggests changes
Reviewer reviews and suggests changes
Developer makes requested changes
Developer makes requested changes
git add .
git commit -m "refactor: improve search efficiency per review"
git push origin feature/search-optimization
git add .
git commit -m "refactor: improve search efficiency per review"
git push origin feature/search-optimization
After approval
After approval
git checkout main
git pull origin main
git merge feature/search-optimization
git push origin main
git checkout main
git pull origin main
git merge feature/search-optimization
git push origin main
Cleanup
Cleanup
git branch -d feature/search-optimization
git push origin -d feature/search-optimization
undefinedgit branch -d feature/search-optimization
git push origin -d feature/search-optimization
undefinedBest Practices
最佳实践
✅ DO
✅ 建议
- Choose workflow matching team size and release cycle
- Keep feature branches short-lived (< 3 days)
- Use descriptive branch names with type prefix
- Require code review before merging to main
- Enforce protection rules on main/release branches
- Rebase frequently to minimize conflicts
- Write atomic, logical commits
- Keep commit messages clear and consistent
- 选择与团队规模和发布周期匹配的工作流
- 保持功能分支生命周期短(< 3天)
- 使用带类型前缀的描述性分支名称
- 合并到主干前要求代码评审
- 对主干/发布分支强制执行保护规则
- 经常变基以减少冲突
- 编写原子化、逻辑清晰的提交
- 保持提交消息清晰一致
❌ DON'T
❌ 禁忌
- Commit directly to main branch
- Create long-lived feature branches
- Use vague branch names (dev, test, temp)
- Merge without code review
- Mix multiple features in one branch
- Force push to shared branches
- Ignore failing CI checks
- Merge with merge commits in TBD
- 直接提交到主干分支
- 创建长期存在的功能分支
- 使用模糊的分支名称(dev、test、temp)
- 未经代码评审就合并
- 在一个分支中混合多个功能
- 强制推送到共享分支
- 忽略失败的CI检查
- 在Trunk-Based Development中使用合并提交
Branch Protection Rules (GitHub)
分支保护规则(GitHub)
yaml
undefinedyaml
undefined.github/branch-protection-rules.yml
.github/branch-protection-rules.yml
branches:
main:
required_status_checks: true
required_code_review: true
dismiss_stale_reviews: true
require_branches_up_to_date: true
enforce_admins: true
required_signatures: false
undefinedbranches:
main:
required_status_checks: true
required_code_review: true
dismiss_stale_reviews: true
require_branches_up_to_date: true
enforce_admins: true
required_signatures: false
undefined