Git不仅是版本控制工具,更是团队协作的基础设施。选择正确的Git工作流,决定了团队的交付效率和代码质量。
1. Git Flow(重型)
main ─────●────●────●────●────●────
│ │
develop ──●──●──●──●──●──●──●──●──
│ │ │
feature ────●─────● │
release ────────────●───●──→ merge to main+develop
hotfix ─────────────────●──→ merge to main+develop
2. GitHub Flow(轻型,推荐)
main ─────●────●────●────●────
│ │
feature ──●────●────● ← PR + Review + CI
3. GitLab Flow(环境分支)
main ──●────●────●────
│ │
staging●────●────●────
│
prod ──●────●────●────
4. Trunk-Based(持续部署)
main/trunk ──●──●──●──●──●──●──●──
│ │ │ │ │ │
short-lived ─● ● ● ● ● ● ← 最多1天
branches feature flags 控制
git commit -S 使用GPG签名# 初始化与配置
git init
git config --global user.name "DevOps Engineer"
git config --global user.email "devops@company.com"
git config --global init.defaultBranch main
# 日常操作
git add -A && git commit -m "feat: add deployment script"
git push origin main
git pull --rebase origin main # 推荐rebase而非merge
# 分支管理
git checkout -b feature/auto-deploy # 创建并切换分支
git branch -a # 查看所有分支
git branch -d feature/old-feature # 删除本地分支
git push origin --delete feature/old # 删除远程分支
# 交互式rebase(整理提交历史)
git rebase -i HEAD~5
# pick → squash(合并提交)
# pick → reword(修改提交信息)
# 暂存工作
git stash save "WIP: deploy script"
git stash list
git stash pop
# 查看历史
git log --oneline --graph --all
git log --author="devops" --since="2026-05-01"
git blame deploy.sh # 查看每行的修改者
# 1. 创建功能分支
git checkout -b feature/monitoring main
# 2. 开发并提交
git add monitoring/
git commit -m "feat: add Prometheus monitoring config"
git push origin feature/monitoring
# 3. 创建Pull Request(通过gh CLI)
gh pr create \
--title "feat: add Prometheus monitoring" \
--body "## 变更内容
- 添加Prometheus配置
- 添加Grafana仪表盘
- 添加告警规则
## 测试
- [x] 本地验证通过
- [x] CI流水线通过"
# 4. Code Review后合并
gh pr merge --squash # 推荐squash合并
git checkout main
git pull origin main
# pre-commit hook(提交前检查)
cat > .git/hooks/pre-commit << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
echo "🔍 运行提交前检查..."
# 检查是否有debug语句
if git diff --cached | grep -E "console\.log|debugger|print\("; then
echo "❌ 发现debug语句,请清理后再提交"
exit 1
fi
# 检查是否有密钥泄露
if git diff --cached | grep -iE "password|secret|api_key|token"; then
echo "⚠️ 可能包含密钥信息,确认后再提交"
read -p "确认提交?(y/N) " -n 1 -r
echo
[[ ! $REPLY =~ ^[Yy]$ ]] && exit 1
fi
# 运行linter
npm run lint 2>/dev/null || true
echo "✅ 提交前检查通过"
EOF
chmod +x .git/hooks/pre-commit
# 使用husky管理hooks(推荐)
npm install husky lint-staged --save-dev
npx husky init
echo "npx lint-staged" > .husky/pre-commit
开发者 Git Server CI/CD
┌──────┐ ┌──────────┐ ┌──────────┐
│ git │──push────▶│ GitHub/ │──webhook▶│ GitHub │
│ clone│ │ GitLab │ │ Actions │
└──────┘ └────┬─────┘ └────┬─────┘
│ │ │
│ PR创建 │ │
├───PR──────────────▶│──触发CI──────────▶│
│ │ │
│ Review + Approve │ │
│◀─────Review───────│ │
│ │ │
│ 合并到main │ │
├───merge───────────▶│──触发CD──────────▶│
│ │ ┌────▼─────┐
│ │ │ 自动部署 │
│ │ │ Staging │
│ │ │ → Prod │
│ │ └──────────┘
# 查看冲突文件
git status
# both modified: config.yaml
# 手动解决冲突
cat config.yaml
# <<<<<<< HEAD
# port: 8080
# =======
# port: 9090
# >>>>>>> feature/new-config
# 编辑文件解决冲突后
git add config.yaml
git commit -m "chore: resolve merge conflict in config"
# 避免冲突:频繁rebase
git fetch origin
git rebase origin/main
# 紧急处理:立即撤销token/密钥!
# 然后从历史中清除
# 使用git filter-branch(慢但通用)
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch secrets.yaml' \
--prune-empty HEAD
# 使用BFG Repo-Cleaner(更快,推荐)
java -jar bfg.jar --delete-files secrets.yaml
git reflog expire --expire=now --all
git gc --prune=now --aggressive
# 预防:使用git-secrets
git secrets --install
git secrets --register-aws
1. Git Flow(重型,适合版本发布产品)
main ─────●────●────●────●────●────
│ │
develop ──●──●──●──●──●──●──●──●──
│ │ │
feature ────●─────● │
release ────────────●───●──→ merge to main+develop
2. GitHub Flow(轻型,推荐,适合持续部署)
main ─────●────●────●────●────
│ │
feature ──●────●────● ← PR + Review + CI
3. Trunk-Based(适合高成熟度团队)
main/trunk ──●──●──●──●──●──●──●──
│ │ │ │ │ │
short-lived ─● ● ● ● ● ● ← 最多1天
branches feature flags 控制
# 1. 创建功能分支
git checkout -b feature/monitoring main
# 2. 开发并提交
git add monitoring/
git commit -m "feat: add Prometheus monitoring config"
git push origin feature/monitoring
# 3. 创建Pull Request
gh pr create \
--title "feat: add Prometheus monitoring" \
--body "## 变更内容
- 添加Prometheus配置
- 添加Grafana仪表盘
- 添加告警规则
## 测试
- [x] 本地验证通过
- [x] CI流水线通过"
# 4. Code Review后合并
gh pr merge --squash # 推荐squash合并
# pre-commit hook(提交前检查)
cat > .git/hooks/pre-commit << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
echo "🔍 运行提交前检查..."
# 检查是否有debug语句
if git diff --cached | grep -E "console\.log|debugger|print\("; then
echo "❌ 发现debug语句,请清理后再提交"
exit 1
fi
# 检查是否有密钥泄露
if git diff --cached | grep -iE "password|secret|api_key|token"; then
echo "⚠️ 可能包含密钥信息,确认后再提交"
fi
echo "✅ 提交前检查通过"
EOF
chmod +x .git/hooks/pre-commit
# 紧急处理:立即撤销token/密钥!
# 然后从历史中清除
# 使用BFG Repo-Cleaner(推荐)
java -jar bfg.jar --delete-files secrets.yaml
git reflog expire --expire=now --all
git gc --prune=now --aggressive
# 预防:使用git-secrets
git secrets --install
git secrets --register-aws
掌握Git工作流、分支策略、PR Review、Git Hooks——这是团队协作和CI/CD的基础。