Forking工作流是开源项目最常用的工作流。

fork在个人仓库创建一份目标远程仓库的副本。如果A拥有一个开源项目,那么想要参与这个项目开发的B和C就可以fork一份仓库副本到自己的个人仓库,B和C可以在本地对这个项目进行开发。开发完成后提交PR,A就会收到这个PR并进行CR,对B进行讨论,CR通过后A就会将B提供的代码合并到A的仓库中。

操作步骤如下:

  1. Fork远程仓库到自己账号下
  2. 克隆仓库到自己本地进行开发,通常会进行如下配置
$ git clone xxx
$ cd repo_name
# 设置源仓库的地址别名为upstream
$ git remote add upstream https://github.com/marmotedu/gitflow-demo
# 设置无法向upstream推送
$ git remote set-url --push upstream no_push # Never push to upstream master
$ git remote -v # Confirm that your remotes make sense
origin  https://github.com/colin404fork/gitflow-demo (fetch)
origin  https://github.com/colin404fork/gitflow-demo (push)
upstream  https://github.com/marmotedu/gitflow-demo (fetch)
upstream  https://github.com/marmotedu/gitflow-demo (push)

之所以设置upstream就是为了方便从源仓库拉取代码,而不是向其push代码。 3. 创建功能分支 首先需要同步本地仓库master分支为最新的状态(与upstream master保持一致)

$ git fetch upstream
$ git checkout master
$ git rebase upstream/master

然后再创建功能分支

git checkout -b feature/add-function
  1. 提交 提交前,需要再次同步upstream/master分支
$ git fetch upstream # commit 前需要再次同步 feature 跟 upstream/master
$ git rebase upstream/master
$ git add <file>
$ git status
$ git commit

将功能分支上的提交合并为一个提交,使用合并提交中的内容

$ git rebase -i origin/master
  1. push功能分支到远程个人仓库
$ git push -f origin feature/add-function
  1. 在个人远程仓库创建PR,要注意,创建PR的时候,base通常选择目标远程仓库的master分支。

tags: 工作流