协同开发是git的核心能力

1.Git-Flow工作流模型

  • Git-Flow模型解决什么问题?为了解决实际项⽬中代码开发、代码测试、bug修复、版本发布等⼀系过程列严重耦合从⽽产⽣各种问题,如冲突过度、版本混乱。
  • Git-Flow模型⼜是如何解决上述问题的呢?基于Git定义5种类型的分⽀,各分⽀严格定义其指责、起⽌点等,从⽽使开发、测试、发版等过程有条不紊进⾏。

    1.1Git-Flow流程图

    该流程图完整描述Git-Flow模型处理过程,当我们深⼊理解各分⽀,然后结合项⽬阶段与⾃身的⻆⾊(开发/测试/项⽬经理),就会发现每个角色在某个阶段需要关注的可能也就⼀两个分⽀,⽐如在开发阶段,开发⼈员只需关注⾃⼰的新功能分⽀(Feature分支);release阶段,测试⼈员和开发⼈员都只需关注Release分⽀,各⾃的职责有所差异⽽已;具体如下图:
    image.png

    1.2Git-Flow各分⽀的说明

    | 分⽀名称 | 作⽤ | ⽣命周期 | 提交or合并 | 起⽌点 | | —- | —- | —- | —- | —- | | Feature分⽀ | ⽤于某个功能的 | 临时分 ⽀、开发 阶段 | 可提交代码 | 由Develop分⽀产⽣, 最终合并到Develop 分⽀ | | Develop分⽀ | 记录历史开发功 能 | 贯穿整个 项⽬ | 不能提交,由Feature分 ⽀、Bugfix分⽀、Release 分⽀、Hotfix分⽀合并代码 | 整个项⽬ | | Release分⽀ | ⽤于本次Release 如⽂档、测试、 bug修复 | 临时分 ⽀、发版 阶段 | 可提交代码 | 由Develop分⽀产⽣, 最终合并到Develop 分⽀和Master分支 | | Hotfix分⽀ | ⽤于解决线上bug | 临时分 ⽀、紧急 修复阶段 | 可提交代码 | 由Master分⽀产⽣, 最终合并到Develop 分⽀和Master分支 | | Master(Production) 分⽀ | 记录历史发布版 本 | 贯穿整个 项⽬ | 不能提交,由Release、Hotfix分⽀合并代码 | 整个项⽬ |

1.3不同角度理解分支

  • ⽣命周期Master分⽀和Develop分⽀贯穿项⽬;其他分⽀均为承担特定指责的临时分⽀。
  • 项⽬阶段开发阶段主要涉及Feature分⽀、Develop分⽀; 发布阶段 主要涉及Release分⽀、Production分⽀、Develop分⽀; 紧急修复阶段 主要涉及Hotfix分⽀、Production分⽀、Develop分⽀。
  • 成员关注点开发⼈员 关注Develop分⽀、Feature分⽀以及特殊阶段关注Hotfix、Release分⽀的bug修复; 测试⼈员 关注 Release分⽀、Hotfix分⽀的功能测试;项⽬经理 关注Production分⽀、Release分⽀。

另外要说明,项⽬阶段在时间纬度有可能重叠.⽐如:release阶段(当前版本)与下各版本的开发阶段可同时存在,因为
当前release阶段的发起同时也就意味着下⼀个release的开发阶段的开始;⼀旦线上出现bug(任何时候都可能出现),
紧急修复阶段就可能与开发阶段、发版阶段重叠…因此,要求团队成员都要理解Git-Flow⼯作流,以及⾃身所处的项

2.演示一个完整的Git-Flow流程

2.1创建分支

  1. touch readme.md
  2. git init
  3. git add .
  4. git commit -m "init"
  5. git checkout -b develop master # 创建并切换develop分支,并关联master

2.1模拟开发阶段

(创建新功能Feature分⽀、实现⼀个⽤户登录模块、然后合并到Develop分⽀、删除功能分⽀)

  1. $ git checkout -b feature-login develop # 新建并切换feature-login分支并关联develop
  2. $ touch LoginUser.html # 创建登陆页面
  3. ……
  4. $ git add .
  5. $ git commit -m "feat:add LoginUser.html"
  6. $ touch LoginUser.js
  7. ……
  8. $ git add.
  9. $ git commit -m "feat: add LoginUser.js"
  10. $ git status
  11. On branch feature-login
  12. nothing to commit, working tree clean
  13. $ git checkout develop
  14. Switched to branch 'develop'
  15. git merge --no-ff feature-login # 合并分支
  16. Merge made by the 'recursive' strategy.
  17. LoginUser.html | 1 +
  18. LoginUser.js | 1 +
  19. 2 files changed, 2 insertions(+)
  20. create mode 100644 LoginUser.html
  21. create mode 100644 LoginUser.js
  22. $ git branch -d feature-login # 删除分支
  23. Deleted branch feature-login (was b0d494c).

2.2模拟Release阶段

(创建Release分⽀、进⾏bug修复、合并到Production分⽀与Develop分⽀)

  1. $ git checkout -b release-v0.1 develop
  2. Switched to a new branch 'release-v0.1'
  3. $ echo "bugifx LoginUser.html" >> LoginUser.html #向文件写入新内容
  4. $ git add .
  5. $ git commit -m "fix: bugfix for LoginUser.html"
  6. $ git checkout master
  7. $ git merge --no-ff release-v0.1
  8. $ git tag v0.1
  9. $ git checkout develop
  10. $ git merge --no-ff release-v0.1
  11. $ git branch -d release-v0.1

2.3 模拟线上故障,创建Hotfix分支

(创建Hotfix分⽀、进⾏bug修复、合并到Production分⽀与Develop分⽀)

  1. $ git checkout -b hotfix-v0.1.1 master
  2. $ git status
  3. On branch hotfix-v0.1.1
  4. nothing to commit, working tree clean
  5. $ echo "hotfix for LoginUser.html" >> LoginUser.html
  6. $ cat LoginUser.html
  7. hi, this is user html
  8. bugifx LoginUser.html
  9. hotfix for LoginUser.html
  10. $ git add .
  11. $ git commit -m "hotfix: do something for LoginUser.html"
  12. $ git checkout master
  13. $ git merge --no-ff hotfix-v0.1.1
  14. $ git tag v0.1.1
  15. $ git checkout develop
  16. $ git merge --no-ff hotfix-v0.1.1
  17. Merge made by the 'recursive' strategy.
  18. LoginUser.html | 1 +
  19. 1 file changed, 1 insertion(+)
  20. $ git branch -d hotfix-v0.1.1
  21. Deleted branch hotfix-v0.1.1 (was bcb680e).