你将收获
- 了解使用 Git 的机制
- 介绍 Git 功能分支工作流程
- 了解 Git 如何鼓励协作
- 成为一名社交程序员
如何成为 Git 绝地武士
规则#1:为每个新项目创建一个 Git 存储库
规则#2:为每个新功能创建一个新分支
规则#3:使用 Pull 请求将代码合并到 Master
Git
- Linus Torvalds 于 2005 年发明的用于 Linux 内核开发的去中心化源代码管理 (SCM) 工具
- 代码保存在存储库中,每个开发人员都有完整的副本
- 在本地工作,无需任何服务器
- 使用 GitHub、GitLab 和 BitBucket 远程工作
Github
- 托管 git 存储库的网站
- 成立于 2007 年,拥有 4000 万注册开发者,2018 年被微软以高达 75 亿美元的价格收购
- 免费和付费帐户
- 增加了跟踪问题和错误的能力
- 提供 webhook 来集成其他工具
Git 是工具,Github 是网站
Git 命令工作流程
Git 功能分支工作流程
- CREATE a new repository or FORK an exiting repository
- CLONE it to your local system
- Create a BRANCH to work on your ISSUE
- COMMIT changes to that branch
- PUSH your changes to the Remote branch
- Issue a PULL REQUEST to have your work reviewed
- MERGE your code to master and close the ISSUE
详细的功能分支工作流程
- CLONE a Repository (FORK first if not part of Dev Team)
- Assign the ISSUE for your work to yourself and place it in working status
- Create a BRANCH to work on an ISSUE
- Run the Test suite to make sure you can run the code
- Make changes to code and test cases and COMMIT to local BRANCH
- Run the Test suite early and often to make sure you didn’t break anything
- PUSH changes to remote BRANCH
- Did we mention testing the code early and often?
- Create PULL REQUEST when all tests pass and code is ready for review / MERGE
示例
- 您的初始工作流程应设置一个可跟踪的远程分支
git checkout master
git pull
git checkout -b my-new-feature
git add .
git commit -m 'initial working version of my new feature'
git push -u origin my-new-feature
- 第二天,您添加更多代码并推送到远程分支
... write some code here ...
git add .
git commit -m 'added this cool code...'
git push
- 在发出 Pull Request 之前,始终从 master 获取最新的更改
git checkout master
git pull
git checkout my-new-feature
git merge master
... fix any merge conflicts here ...
git add .
git commit -m 'merged updates from master'
git push
- 合并 Pull Request 后
git checkout master
git pull
git branch -d my-old-feature
git checkout -b my-new-feature
- 删除旧分支
- 为新功能创建一个新分支