Git 分支管理最佳实践
Git 是分布式版本控制系统,掌握分支管理是团队协作的关键。本文将全面介绍 Git 分支管理的最佳实践。
一、分支基础
1.1 什么是分支?
分支允许你在开发过程中创建一个独立的分支进行修改,不会影响主分支。
1.2 创建分支
git checkout -b feature/new-feature
git switch -c feature/new-feature
git branch feature/new-feature
git branch feature/new-feature abc123
|
1.3 查看分支
git branch
git branch -a
git branch --show-current
git branch -vv
|
1.4 切换分支
git checkout feature/new-feature
git switch feature/new-feature
git switch -
git switch origin/feature/new-feature
|
1.5 删除分支
git branch -d feature/old-feature
git branch -D feature/old-feature
git push origin --delete feature/old-feature git push origin :feature/old-feature
|
二、分支工作流
2.1 Git Flow
main (生产环境) ├── develop (开发环境) ├── feature/user-auth ├── feature/payment └── bugfix/login-error
|
创建和合并
git checkout develop git checkout -b feature/user-auth
git add . git commit -m "feat: 添加用户认证功能" git checkout develop git merge feature/user-auth
git branch -d feature/user-auth
|
2.2 GitHub Flow
main (主分支) ├── feature/user-auth ├── feature/payment └── hotfix/critical-bug
|
创建和合并
git checkout main git pull origin main git checkout -b feature/user-auth
git add . git commit -m "feat: 添加用户认证功能" git push origin feature/user-auth
git checkout main git pull origin main git branch -d feature/user-auth
|
2.3 Gitlab Flow
main (主分支) ├── feature/user-auth └── hotfix/critical-bug
|
创建和合并
git checkout main git pull origin main git checkout -b feature/user-auth
git checkout main git pull origin main git branch -d feature/user-auth
|
三、分支操作
3.1 分支合并
git merge feature/new-feature
git merge --no-ff feature/new-feature
git merge --no-commit feature/new-feature
git merge --abort
git add .
git commit
|
3.2 分支变基
git rebase main
git rebase --continue
git rebase --skip
git rebase --abort
git rebase -i HEAD~3
pick abc123 Feature 1 squash def456 Feature 2 pick ghi789 Feature 3
|
3.3 分支追踪
git branch --set-upstream-to=origin/main feature/new-feature
git push -u origin feature/new-feature
git branch -vv
|
3.4 远程分支
git branch -a
git remote show origin
git push origin --delete feature/new-feature
git fetch origin
git merge origin/feature/new-feature
git push origin :old-branch new-branch
|
四、分支管理最佳实践
4.1 命名规范
feature/user-auth feature/payment-integration feature/user-profile-edit
bugfix/login-error fix/memory-leak fix/critical-api-error
release/v1.2.0 release/v2.0.0-beta
hotfix/critical-security-patch hotfix/performance-issue
research/new-feature research/optimization
|
4.2 提交信息规范
git commit -m "feat: 添加用户认证功能" git commit -m "feat: 实现支付功能"
git commit -m "fix: 修复登录错误" git commit -m "fix: 修复内存泄漏问题"
git commit -m "docs: 更新 README 文档" git commit -m "docs: 添加 API 文档"
git commit -m "refactor: 重构用户模块" git commit -m "refactor: 优化数据库查询"
git commit -m "perf: 优化查询性能" git commit -m "perf: 减少内存占用"
git commit -m "test: 添加单元测试" git commit -m "test: 更新集成测试"
git commit -m "build: 更新构建配置" git commit -m "ci: 更新 CI 配置"
git commit -m "style: 代码格式化" git commit -m "chore: 更新依赖"
|
4.3 开发流程
1. 拉取最新代码
git checkout main git pull origin main
git checkout develop git pull origin develop
|
2. 创建功能分支
git checkout -b feature/user-auth
|
3. 开发和提交
git add .
git commit -m "feat: 添加用户认证功能"
|
4. 保持分支更新
git checkout feature/user-auth git checkout develop git pull origin develop git checkout feature/user-auth git rebase develop
|
5. 合并到 develop
git checkout develop git merge feature/user-auth git push origin develop
git branch -d feature/user-auth
|
4.4 代码审查
git push origin feature/user-auth
|
五、冲突解决
5.1 检测冲突
git merge feature/new-feature
|
5.2 查看冲突
git status
<<<<<<< HEAD 你的代码 ======= 他人的代码 >>>>>>> feature/new-feature
|
5.3 解决冲突
git add file.txt
git commit
git merge --abort
|
5.4 解决冲突的技巧
git checkout --ours file.txt git checkout --theirs file.txt
git mergetool
|
六、高级操作
6.1 Cherry-pick
git cherry-pick abc123
git cherry-pick abc123 def456 ghi789
git cherry-pick abc123..ghi789
git cherry-pick -e abc123
|
6.2 Rebase 技巧
git rebase -i HEAD~3
git rebase -i HEAD~3
git rebase -i HEAD~3
|
6.3 分支追踪
git branch -vv
git branch -u origin/feature/new-feature
git fetch --all git branch --merged git branch --no-merged
|
七、团队协作
7.1 分支保护
7.2 代码审查流程
git checkout -b feature/user-auth
git add . git commit -m "feat: 添加用户认证功能" git push -u origin feature/user-auth
git push
git checkout main git pull origin main git branch -d feature/user-auth
|
7.3 协作规范
git checkout develop git pull origin develop
git checkout -b feature/new-feature
git fetch origin develop git rebase develop
git status git diff
git fetch origin develop git merge develop
git push origin feature/new-feature
|
八、总结
8.1 分支管理核心要点
- 功能分支:每个功能独立开发
- 主分支:保持稳定
- 定期同步:保持分支最新
- 代码审查:保证代码质量
- 及时清理:删除已合并分支
8.2 常用命令
git branch git checkout -b feature/new-feature git switch feature/new-feature git branch -d feature/new-feature
git merge feature/new-feature git rebase main
git push -u origin feature/new-feature git pull origin feature/new-feature
git status git add . git commit
|
8.3 最佳实践
- 命名规范:清晰易懂的分支名
- 提交信息:遵循规范的提交格式
- 代码审查:通过 PR 审查
- 定期同步:保持分支更新
- 及时清理:删除已合并分支
掌握 Git 分支管理,提升团队协作效率!