使用github的主要目的肯定是為了多人可以進(jìn)行協(xié)作開發(fā),這一部分首先簡單介紹一下git協(xié)作開發(fā)的基礎(chǔ),下一部分再來深入探討基于分支開發(fā)的模式。
設(shè)置協(xié)同開發(fā)人員
在這里我們分別使用兩個(gè)用戶來模擬整個(gè)開發(fā)的流程,此處使用ynkonghao和kh121兩個(gè)用戶來進(jìn)行模擬。
首先使用kh121創(chuàng)建一個(gè)項(xiàng)目p1,創(chuàng)建時(shí)選擇初始化操作。創(chuàng)建完畢之后需要做的第一步是把協(xié)作的開發(fā)人員加入到項(xiàng)目中,選擇settings

之后選擇Collaborators添加合作者,將ynkonghao這個(gè)用戶添加進(jìn)去

添加完成之后,提示需要等待對(duì)方進(jìn)行驗(yàn)證。

如果對(duì)方打開了通知提醒就會(huì)在頁面上有一個(gè)提醒的消息,如果沒有打開通知提醒,就需要等待郵件提醒

此時(shí)選擇接受邀請(qǐng)之后,就可以開始進(jìn)行協(xié)同開發(fā)。


協(xié)同開發(fā)的基本流程
首先分別用兩個(gè)用戶clone網(wǎng)絡(luò)中的項(xiàng)目,首先使用kh121用戶在項(xiàng)目中添加一些內(nèi)容,之后完成提交。
##從遠(yuǎn)程工廠克隆p1項(xiàng)目
[root@localhost test]# git clone https://github.com/kh121/p1
Cloning into 'p1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@localhost test]# cd p1
##創(chuàng)建一個(gè)文件夾,添加一些文件
[root@localhost p1]# mkdir kh121
[root@localhost p1]# cd kh121
[root@localhost kh121]# echo a > a.txt
[root@localhost kh121]# cd ..
##提交版本
[root@localhost p1]# git add .
[root@localhost p1]# git commit -m "kh121 first commit"
[master 829fff8] kh121 first commit
1 file changed, 1 insertion(+)
create mode 100644 kh121/a.txt
##推送到服務(wù)器端
[root@localhost p1]# git push
此時(shí)查詢一下github上的信息,發(fā)現(xiàn)已經(jīng)完成了一次提交

接著使用另外一個(gè)用戶ynkonghao進(jìn)行一些修改之后提交,注意此時(shí)遠(yuǎn)程的版本已經(jīng)修改,而ynkonghao這個(gè)用戶的本地版本依然沒有獲取到遠(yuǎn)程版本的信息。
E:/study/git_2016/11/p1>git push origin master
To github.com:kh121/p1
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:kh121/p1'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
提示提交失敗,因?yàn)檫h(yuǎn)程的版本和我們自己的版本不一樣,所以在提交之前需要進(jìn)行pull操作,先完成本地版本和遠(yuǎn)程版本的同步。
E:\study\git_2016\11\p1>git pull ##同步遠(yuǎn)程版本
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From github.com:kh121/p1
17de85d..829fff8 master -> origin/master
Merge made by the 'recursive' strategy.
kh121/a.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 kh121/a.txt
E:\study\git_2016\11\p1>git lg ##通過log可以發(fā)現(xiàn)會(huì)下載遠(yuǎn)程版本,并且合并到自己的版本中,而且遠(yuǎn)程的版本是有kh121提交的
* 60d9083 - (HEAD -> master) Merge branch 'master' of github.com:kh121/p1 (4 seconds ago) <ynkonghao>
|\
| * 829fff8 - (origin/master, origin/HEAD) kh121 first commit (23 hours ago) <Your Name>
* | fc0c200 - ynkonghao first commit (23 hours ago) <ynkonghao>
|/
* 17de85d - Initial commit (23 hours ago) <kh121>
執(zhí)行了git pull之后會(huì)從github上下載遠(yuǎn)程的版本下來,之后和本地的分支進(jìn)行合并,接著再通過push就可以完成提交。
E:\study\git_2016\11\p1>git push origin master
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 672 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To github.com:kh121/p1
829fff8..60d9083 master -> master
此時(shí)另外一個(gè)用戶如果希望提交他的版本,同樣也需要進(jìn)行pull操作,所以在實(shí)際的應(yīng)用中,我們每一次push操作都需要先進(jìn)行pull,從遠(yuǎn)程倉庫把數(shù)據(jù)拉取下來。這個(gè)操作其實(shí)和svn類似,在commit之前需要進(jìn)行update。同樣也無法避免沖突問題。
遠(yuǎn)程協(xié)作的沖突問題
原則上每個(gè)用戶都應(yīng)該只負(fù)責(zé)自己的模板,這樣盡可能避免沖突,但在實(shí)際的開發(fā)中依然無法完全避免沖突,接下來演示一下沖突的情況,首先讓兩個(gè)用戶的版本都同步

目前兩個(gè)用戶的版本完全一致,此時(shí)在kh121用戶中,修改common文件夾中的文件內(nèi)容。
[root@localhost p1]# cd common
[root@localhost common]# ls
a.txt
[root@localhost common]# echo aaa >> a.txt ##添加一些內(nèi)容到a.txt中
[root@localhost common]# cd ..
[root@localhost p1]# git add .
[root@localhost p1]# git commit -m "common update by kh121"
[master b374a56] common update by kh121
1 file changed, 1 insertion(+)
[root@localhost p1]# git push origin maste
此時(shí)讓ynkonghao的文件夾同樣也修改common中的a.txt,然后進(jìn)行pull,這里故意讓a.txt的相同位置進(jìn)行了修改,所以就會(huì)出現(xiàn)沖突
E:\study\git_2016\11\p1\common>echo aa >> a.txt
E:\study\git_2016\11\p1\common>more a.txt ###在相同的位置進(jìn)行了修改
a
aa
###完成了提交操作
E:\study\git_2016\11\p1\common>cd ..
E:\study\git_2016\11\p1>git add .
E:\study\git_2016\11\p1>git commit -m "update common by ynkonghao"
[master 46bde5f] update common by ynkonghao
2 files changed, 2 insertions(+)
create mode 100644 a.txt
create mode 100644 common/a.txt
###進(jìn)行了pull操作之后,發(fā)現(xiàn)了沖突
E:\study\git_2016\11\p1>git pull
Auto-merging common/a.txt
CONFLICT (add/add): Merge conflict in common/a.txt
Automatic merge failed; fix conflicts and then commit the result.
E:\study\git_2016\11\p1>cd common
###顯示了aa這兩個(gè)字符是在本地的header版本中編寫的,而aaa是在id為b374xx的版本中編寫的。
E:\study\git_2016\11\p1\common>more a.txt
a
<<<<<<< HEAD
aa
=======
aaa
>>>>>>> b374a565af1a1287394a2e079ae2e0708719e168
這種沖突問題就得由開發(fā)人員手動(dòng)解決,整個(gè)流程和svn基本類似,如果僅僅只是按照上述的開發(fā)進(jìn)行版本管理,在一個(gè)容易協(xié)調(diào)的團(tuán)隊(duì)中是沒有問題的,但是如果在互聯(lián)網(wǎng)中就會(huì)感覺版本稍微有些混亂,此時(shí)利用好分支模型就比較的重要,下一部分將會(huì)詳細(xì)講解基于分支模型的開發(fā)。
這部分的內(nèi)容講解了如何進(jìn)行協(xié)作開發(fā),并且分析了協(xié)作開發(fā)的流程,整個(gè)流程和svn類似,都是在push之前進(jìn)行pull操作。