用开源代码库github协同开发常见的操作流程:
以比特币源码为例:
1.fork代码仓库
先在自己的github账号下fork代码仓库,然后在终端进行如下操作:
git clone git@github.com:your_username/bitcoin.git
cd bitcoin
git remote set-url origin git@github.com:your_username/bitcoin.git
git remote add upstream git@github.com:bitcoin/bitcoin.git
git remote set-url --push upstream DISABLED
git fetch upstream
git branch -u upstream/master master
注:your_username为你自己的github名称
上述操作完成后,你的.git/config文件应当是如下格式:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:your_username/bitcoin.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = upstream
merge = refs/heads/master
[remote "upstream"]
url = git@github.com:bitcoin/bitcoin.git
fetch = +refs/heads/*:refs/remotes/upstream/*
pushurl = DISABLED
2.创建分支
在项目中,我们需要修改bug、曾删代码等操作,因此需要创建一个新的分支。
假设现在在bitcoin目录中:
git checkout -b [new_branch_name]
注:在push之前,此分支仅在本地仓库中,github上并不会显示。
chechout已有的分支:
git checkout [existing_branch_name]
3.提交更改
为了防止和master分支冲突,在提交之前,需要rebase确保没有冲突:
git fetch upstream
git rebase upstream/master
git push -f
如果有未提交的更改,可以使用git隐藏来保存它们:
git stash
git fetch upstream
git rebase upstream/master
git push -f
git stash pop
一旦与master同步,提交我们的更改:
git add [files...] # default is all files, be careful not to add unintended files
git commit -m 'Message describing commit'
git push
此时已提交完更改。
4.创建pull request
去github页面提交Pull Request
5.Discuss / Review PR
此时仓库管理员会评审代码,选择是否接受PR