GitHub多人协作简明教程
Posted idorax
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GitHub多人协作简明教程相关的知识,希望对你有一定的参考价值。
本文面向了解git的基本命令但是不熟悉如何使用GitHub进行多人协作开发项目的同学。
为简单起见,这里只设定有两个开发人员,HuanianLi 和 DaxiangLi。他们在GitHub上的地址为:
- HuanianLi: https://github.com/huanianli # host developer
- DaxiangLi: https://github.com/daxiangli # guest developer
HuanianLi将创建一个项目kaiba, 然后邀请DaxiangLi来一起开发。
1. 在GitHub上创建两个帐号(HuanianLi and DaxiangLi),(这里以HuanianLi为例)
1.1 打开GitHub的主页: https://github.com/
1.2 点击右上角的Sign up
1.3 跳转到注册页面后,开始Create your personal account,按照要求输入Username, Email Address, Password, 例如:
1.4 点击页面下方的"Create an account"按钮并按照后续提示完成注册。
2. 在Linux桌面上创建两个用户(huanianli and daxiangli), (这里以huanianli为例)
2.1 创建用户huanianli
[email protected]:~# useradd -d /home/huanianli -m -s /bin/bash -c "Huanian Li" huanianli
2.2 对用户huanianli的git进行设置 (这里直接编辑文件~/.gitconfig)
[email protected]:~$ vim ~/.gitconfig [email protected]:~$ cat -n ~/.gitconfig 1 [user] 2 email = [email protected] 3 name = Huanian Li 4 [core] 5 editor = vim 6 [push] 7 default = matching [email protected]:~$ [email protected]:~$ git config -l user.email=[email protected] user.name=Huanian Li core.editor=vim push.default=matching [email protected]:~$
2.3 在huanianli的HOME目录下创建一个工作空间workspace
[email protected]:~$ cd ~ [email protected]:~$ mkdir workspace [email protected]:~$ ls workspace [email protected]:~$
3. (Host Developer) HuanianLi在GitHub上创建一个项目kaiba
3.1 登录HuanianLi的GitHub空间,打开页面Repositories, e.g.
https://github.com/HuanianLi?tab=repositories
点击页面右边的New按钮, 进入创建一个新的代码仓库的页面。
点击"Create repository"完成创建。
3.2 进入HuanianLi的Linux桌面,clone在3.1创建的项目kaiba
[email protected]:~$ cd workspace [email protected]:~/workspace$ git clone https://github.com/HuanianLi/kaiba.git Cloning into ‘kaiba‘... warning: You appear to have cloned an empty repository. Checking connectivity... done. [email protected]:~/workspace$ ls kaiba [email protected]:~/workspace$ cd kaiba [email protected]:~/workspace/kaiba$ vi README.md [email protected]:~/workspace/kaiba$ cat -n README.md 1 kaiba: A sandbox project of Kaiba [email protected]:~/workspace/kaiba$ [email protected]:~/workspace/kaiba$ git add README.md [email protected]:~/workspace/kaiba$ [email protected]:~/workspace/kaiba$ git commit -m "Initialize the project by adding README.md" [master (root-commit) c0bd2ed] Initialize the project by adding README.md 1 file changed, 1 insertion(+) create mode 100644 README.md [email protected]:~/workspace/kaiba$ [email protected]:~/workspace/kaiba$ git log commit c0bd2ed35e10180209726199af422a63a007355f Author: Huanian Li <[email protected]> Date: Wed Jul 25 16:21:43 2018 +0800 Initialize the project by adding README.md [email protected]:~/workspace/kaiba$ [email protected]:~/workspace/kaiba$ git branch * master [email protected]:~/workspace/kaiba$ [email protected]:~/workspace/kaiba$ git push -u origin master Username for ‘https://github.com‘: HuanianLi Password for ‘https://[email protected]‘: Counting objects: 3, done. Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/HuanianLi/kaiba.git * [new branch] master -> master Branch master set up to track remote branch master from origin. [email protected]:~/workspace/kaiba$
在上面的操作中,关键的步骤是:
- clone
git clone https://github.com/HuanianLi/kaiba.git
- push
git push -u origin master
注意: 因为是第一次push, ‘-u’ 是必须的。
3.3 一旦完成git push之后,我们就可以浏览GitHub上的项目了
4. (Guest Developer) DaxiangLi登录他的GitHub, 然后进入(Host Developer) HuanianLi的GitHub空间,fork项目kaiba
4.1 DaxiangLi登录他的GitHub
4.2 进入(Host Developer) HuanianLi的GitHub空间, 点开项目kaiba
注意: 目前的 Fork 为0。
4.3 fork项目kaiba, 直接点Fork按钮
Fork 结束后, DaxiangLi的GitHub里就有了项目kaiba
5. (Guest Developer)DaxiangLi贡献代码给项目kaiba
5.1 DaxiangLi在他的Linux桌面上clone代码
[email protected]:~/workspace$ git clone https://github.com/DaxiangLi/kaiba.git Cloning into ‘kaiba‘... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Checking connectivity... done.
两个问题:
- DaxiangLi 能直接clone HuanianLi的GitHub里的代码吗? A: 能。
- DaxiangLi 能直接clone HuanianLi的GitHub里的代码,然后做修改后push回去吗? A: 不能。
5.2 DaxiangLi新建一个开发分支
[email protected]:~/workspace/kaiba$ git checkout -b dev Switched to a new branch ‘dev‘
其中, git checkout -b dev 等同于
1 git branch dev 2 git checkout dev
5.3 把HuanianLi的项目添加到DaxiangLi的远程仓库
[email protected]:~/workspace/kaiba$ git remote add upstream https://github.com/HuanianLi/kaiba.git
5.4 将HuanianLi的分支取下来
[email protected]:~/workspace/kaiba$ git remote update Fetching origin Fetching upstream From https://github.com/HuanianLi/kaiba * [new branch] master -> upstream/master
5.5 将HuanianLi的master分支更新到本地
[email protected]:~/workspace/kaiba$ git fetch upstream master From https://github.com/HuanianLi/kaiba * branch master -> FETCH_HEAD
5.6 合并HuanianLi的分支到本地(注意:这一步在第一次其实不需要)
[email protected]:~/workspace/kaiba$ git rebase upstream/master Current branch dev is up to date.
5.7 DaxiangLi添加一个文件并提交commit
[email protected]:~/workspace/kaiba$ git add foo.py [email protected]:~/workspace/kaiba$ git commit -m "DaxiangLi: Add foo.py" [email protected]:~/workspace/kaiba$ git push -u origin dev
5.8 进入DaxiangLi的GitHub, 提交一个PR
Q: 什么是PR?
A: PR是Pull Request的缩写,是开发者使用GitHub进行协作的利器。简单来说,Pull Request是一种机制,让开发者告诉项目成员一个功能已经完成。一旦feature分支开发完毕,开发者使用GitHub账号提交一个Pull Request。它告诉所有参与者,他们需要审查代码,并将代码并入master分支。Pull Request不只是一个通知,还是一个专注于某个提议功能的讨论版面。Pull Request 需要两个不同的分支或是两个不同的仓库, 其工作原理大概是:
- 开发者在他们的本地仓库中为某个功能创建一个专门的分支;
- 开发者将分支推送到公共的GitHub仓库;
- 开发者用GitHub发起一个Pull Request;
- 其余的团队成员审查代码,讨论并且做出修改;
- 项目维护者将这个功能并入官方的仓库,然后关闭这个Pull Request。
现在点击Compare & pull request按钮,就创建一个PR,
点页面下方的"Create pull request"按钮即可。 生成的PR看起来是这样的:
6. (Host Developer) HuanianLi 查看PR并Merge
6.1 HuanianLi查看PR
HuanianLi可能在PR中加入comment, 要求DaxiangLi做相应的修改。 这里为简单起见,直接Review通过。
6.2 Huanli 点Merge pull request
到此为止,DaxiangLi成功地给HuanianLi贡献了一次代码!! 当然,这次贡献过程异常简单,因为未涉及到冲突解决。接下来将介绍更复杂的情况,这才是重点:-)
7. HuanianLi 查看kaiba并删除无用的commit
7.1 使用git pull更新本地空间
[email protected]:~/workspace/kaiba$ git pull remote: Counting objects: 4, done. remote: Compressing objects: 100% (4/4), done. remote: Total 4 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), done. From https://github.com/HuanianLi/kaiba c0bd2ed..b8a0b63 master -> origin/master Updating c0bd2ed..b8a0b63 Fast-forward foo.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 foo.py
注意: 在顶端的commit不是我们想要的,需要把它移除掉。
commit b8a0b63d8b18f8466e79284ed6022340ca5a43a8 Merge: c0bd2ed 640f6f6 Author: Huanian Li <41667254[email protected]> Date: Wed Jul 25 18:55:02 2018 +0800 Merge pull request #1 from DaxiangLi/dev DaxiangLi: Add foo.py
7.2 使用git rebase -i移除不需要的commit
[email protected]:~/workspace/kaiba$ git rebase -i c0bd2ed35e10180209726199af422a63a007355f Successfully rebased and updated refs/heads/master.
在弹出的交互界面中不做任何修改,直接保存退出即可。
7.3 使用git push --force强制更新远端的master分支
[email protected]:~/workspace/kaiba$ git push --force Username for ‘https://github.com‘: HuanianLi Password for ‘https://[email protected]‘: Total 0 (delta 0), reused 0 (delta 0) To https://github.com/HuanianLi/kaiba.git + b8a0b63...640f6f6 master -> master (forced update)
这下清爽啦!!
。。。TBD .....
参考资料:
以上是关于GitHub多人协作简明教程的主要内容,如果未能解决你的问题,请参考以下文章