协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
git-branching-strategies【开发与工程】"Git branching models including Git Flow, GitHub Flow, Trunk-Based Development" and feature flag strategies for CI/CD pipelines
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: git-branching-strategies compatibility: opencode completeness: 95 content-types:
Patterns for managing code changes through branches, merges, and feature flags in collaborative development environments.
| Model | Branches | Release Cadence | Best For |
|---|---|---|---|
| Git Flow | develop, release/, hotfix/ | Fixed releases | Traditional projects, scheduled releases |
| GitHub Flow | main, feature/* | Continuous deployment | Web apps, SaaS products |
| Trunk-Based | main (mainline) | Multiple releases/day | DevOps teams, rapid iteration |
| Feature Flag | main | Any | Large teams, progressive delivery |
main ──●───────●───────●───────●
│ │ │ │
release/1.0.0 ●───────● │ │
│ │ │ │
develop ●───────●───────●───────●
│ ┌───┘ │ │
feature/auth ●───● │ │
│ │ ┌───────┘ │
feature/payments ●───●───● │
│ │ │ ┌───────────┘
hotfix/login-bug ●───●───●───●
| Branch | Purpose | Lifetime | Push Rights |
|---|---|---|---|
main | Production-ready code | Permanent | Maintainers |
develop | Integration branch | Permanent | Developers |
feature/* | New features | Temporary | Feature owner |
release/* | Release preparation | Temporary | Release manager |
hotfix/* | Emergency fixes | Temporary | Anyone |
# 1. Start a new feature
git checkout develop
git checkout -b feature/auth
# 2. Work on feature
git commit -m "Add authentication endpoints"
git push origin feature/auth
# 3. Complete feature
git checkout develop
git merge --no-ff feature/auth -m "Merge feature/auth into develop"
git push origin develop
# 4. Start release
git checkout -b release/1.0.0 develop
git push origin release/1.0.0
# 5. Finish release
git checkout main
git merge --no-ff release/1.0.0 -m "Release 1.0.0"
git tag -a v1.0.0 -m "Version 1.0.0"
git push origin v1.0.0
git checkout develop
git merge release/1.0.0
# 6. Hotfix (from main)
git checkout main
git checkout -b hotfix/login-bug
git commit -m "Fix login bug"
git checkout main
git merge --no-ff hotfix/login-bug
git tag -a v1.0.1 -m "Version 1.0.1"
Pros:
main branchCons:
developmain ●───────●───────●───────●
│ │ │ │
feature/1 ●───● │ │ │
│ │ │ │ │
feature/2 ●─● │ │ │
│ │ │ │ │
feature/3 ●───●───●───●───●───●
main branch is always deployablemainmain when readymain# 1. Create feature branch
git checkout main
git pull origin main
git checkout -b feature/new-dashboard
# 2. Commit changes
git add .
git commit -m "Add dashboard component"
git push origin feature/new-dashboard
# 3. Open Pull Request
# Review, address feedback
# Merge when approved
# 4. Deploy
# CI/CD automatically deploys from main
Pros:
Cons:
main branchmain ●───●───●───●───●───●───●───●
│ │ │ │ │ │ │ │
│ │ │ │ │ │ │ └─ Feature E (feature flag)
│ │ │ │ │ │ └───── Feature D (feature flag)
│ │ │ │ │ └───────── Feature C (feature flag)
│ │ │ │ └───────────── Feature B (feature flag)
│ │ │ └───────────────── Feature A (feature flag)
│ │ └───────────────────── Hotfix
│ └───────────────────────── Release
└───────────────────────────── Release
main or short-lived branches# Option 1: Direct to main (small changes)
git checkout main
git pull origin main
# Work on change
git commit -m "Fix typo in documentation"
git push origin main
# Option 2: Short-lived feature branch (larger changes)
git checkout main
git pull origin main
git checkout -b feature/payment-integration
# Commit frequently with small, atomic commits
git commit -m "Add payment API client"
git commit -m "Integrate payment API"
git commit -m "Add feature flag for payments"
git checkout main
git pull origin main
git merge feature/payment-integration
git push origin main
# config/features.py
class FeatureFlags:
def __init__(self):
self.flags = {
"new_payment": False,
"dark_mode": False,
"beta_features": False,
}
def is_enabled(self, flag_name: str) -> bool:
return self.flags.get(flag_name, False)
# In your code
def process_payment(user, amount):
if feature_flags.is_enabled("new_payment"):
return new_payment_processor.process(user, amount)
else:
return legacy_payment_processor.process(user, amount)
Pros:
Cons:
# Toggle feature availability
feature_flags = {
"new_checkout": True, # Enabled
"dark_mode": False, # Disabled
}
# Roll out to subset of users
def should_show_beta_feature(user_id: int) -> bool:
return user_id % 10 == 0 # 10% of users
def get_variant(user_id: int) -> str:
return "A" if hash(user_id) % 2 == 0 else "B"
# Usage
variant = get_variant(user.id)
if variant == "A":
show_old_checkout()
else:
show_new_checkout()
# .github/workflows/release.yml
name: Release
on:
push:
branches:
- main
pull_request:
branches:
- develop
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm test
- run: npm run lint
deploy-staging:
needs: test
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh staging
deploy-production:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh production
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches:
- main
jobs:
test-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: npm test
- name: Deploy to production
if: success()
run: ./deploy.sh production
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
# .github/workflows/ci-cd.yml
name: CI/CD
on:
push:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npm test
deploy:
needs: build-and-test
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to production
run: ./deploy.sh production
| Project Type | Team Size | Release Frequency | Recommended Model |
|---|---|---|---|
| Web app/SaaS | 1-5 | Daily/Weekly | GitHub Flow |
| Web app/SaaS | 5-20 | Daily/Weekly | Trunk-Based + Flags |
| Mobile app | 1-10 | Monthly | Git Flow |
| Library/SDK | 2-10 | Quarterly | Git Flow |
| Enterprise | 10-50 | Monthly | Git Flow |
| DevOps team | 5-15 | Multiple/day | Trunk-Based |
How often do we need to release?
What's our team size?
Do we use feature flags?
# BAD: Feature branch exists for weeks
git checkout -b feature/new-frontend
# Commits over 3 weeks
# Merge conflicts with main
# Integration stress
# GOOD: Small, frequent merges
git checkout main
git pull origin main
git checkout -b feature/login-form
# Complete in 1-2 days
git push origin feature/login-form
# BAD: No pull requests, direct commits
git checkout main
git commit -m "Quick fix"
git push origin main
# No review, no testing
# GOOD: PR workflow
git checkout -b fix/login-bug
git commit -m "Fix login bug"
git push origin fix/login-bug
# Open PR, get review, merge
When implementing a branching strategy, provide: