Github flow: https://guides.github.com/introduction/flow/
背景介绍
在团队协作中,通常都需要一个统一的 Workflow
来保证高效且稳定的开发,本文主要参考 Github flow。
假设
以上假设是我们接下来操作的前提。
约定
-
master
分支作为一个项目开发的主分支,只要有新的Pull Request(合并请求,以下简称为 PR)
(或Merge Request(合并请求,以下简称为 MR)
)合并到master
分支,就会触发代码的自动构建,并自动部署至测试环境中。唯有手动创建tag
,才会触发线上环境的自动构建并部署或者Release
。 -
任何人(包括
Owner
和Maintainer
)都不能直接push
代码到master
分支(不能拥有此权限),且PR(MR)
的代码必须通过CI/CD
才允许合并至master
分支。
Fork 项目
在贡献代码之前,fork
项目到个人仓库。
Clone 项目
git clone git@github.com:JohnNiang/halo.git
cd halo
设置 Remote
执行下条命令可以查看当前 Remote
状态:
git remote -v
# 结果
origin git@github.com:JohnNiang/halo.git (fetch)
origin git@github.com:JohnNiang/halo.git (push)
此时,本地仓库的 Remote
默认为 origin
,我们需要添加一个主仓库的 Remote
,方便我们同步主仓库最新的代码。
git remote add upstream git@github.com:halo-dev/halo.git
# 可重复执行下条命令查询当前 Remote 状态
git remote -v
# 结果
origin git@github.com:JohnNiang/halo.git (fetch)
origin git@github.com:JohnNiang/halo.git (push)
upstream git@github.com:halo-dev/halo.git (fetch)
upstream git@github.com:halo-dev/halo.git (push)
说明:
Remote
的名称:origin
和upstream
并非固定,可自定义。
拉取主仓库的 master 分支
拉取主仓库下的 master
分支。
git fetch upstream master
# 结果
remote: Enumerating objects: 91, done.
remote: Counting objects: 100% (91/91), done.
remote: Total 186 (delta 91), reused 91 (delta 91), pack-reused 95
Receiving objects: 100% (186/186), 42.16 KiB | 204.00 KiB/s, done.
Resolving deltas: 100% (96/96), completed with 52 local objects.
From github.com:halo-dev/halo
* branch master -> FETCH_HEAD
* [new branch] master -> upstream/master
当然也可以通过 git fetch --all
拉取所有仓库的所有内容。
从主仓库的 master 分支创建新的分支
首先进入主仓库的 master
分支,从该分支切出一个我们需要开发的分支。
git checkout upstream/master
# 结果
Note: switching to 'upstream/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 9f65092b doc: update CHANGELOG.md.
创建自己的分支:
git checkout -b chore/document
# 结果
Switched to a new branch 'chore/document'
Push 到个人仓库
git push origin chore/document
# 结果
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'chore/document' on GitHub by visiting:
remote: https://github.com/JohnNiang/halo/pull/new/chore/document
remote:
To github.com:JohnNiang/halo.git
* [new branch] chore/document -> chore/document
推送完成后可进行正式开发,建议频繁 commit
(保证 commit
的粒度足够小)以及 push
到个人仓库。
提交 PR(MR)
- 在进行各种
add
、commit
和push
操作后(开发完成),可以开始向主仓库提交PR(MR)
,等待主仓库的成员 Review 代码。 - 如果代码需要修改的,回到第一步。
PR(MR)
审核通过,由主仓库的成员合并该请求,整个代码贡献结束。