1. 程式人生 > >12.GIT多人協作

12.GIT多人協作

地址 ont 並不是 delet total same git add using remote

當你從遠程倉庫克隆時,實際上Git自動把本地的master分支和遠程的master分支對應起來了,並且,遠程倉庫的默認名稱是origin.
查看遠程庫的信息
$ git remote
origin

$ git remote -v
origin  https://github.com/apollo1616/test.git (fetch)
origin  https://github.com/apollo1616/test.git (push)
# 上面顯示了可以抓取和推送的origin的地址.如果沒有推送權限,就看不到push的地址.

1 推送分支

推送分支,就是把該分支上的所有本地提交推送到遠程庫.推送時,要指定本地分支,這樣,Git就會把該分支推送到遠程庫對應的遠程分支上:
$ git push origin master

如果要推送其他分支,比如dev,就改成:
$ git push origin dev
但是,並不是一定要把本地分支往遠程推送,那麽,哪些分支需要推送,哪些不需要呢?
master分支是主分支,因此要時刻與遠程同步;
dev分支是開發分支,團隊所有成員都需要在上面工作,所以也需要與遠程同步;
bug分支只用於在本地修復bug,就沒必要推到遠程了,除非老板要看看你每周到底修復了幾個bug;
feature分支是否推到遠程,取決於你是否和你的小夥伴合作在上面開發.
總之,就是在Git中,分支完全可以在本地自己藏著玩,是否推送,視你的心情而定!

2 抓取分支

多人協作時,大家都會往master和dev分支上推送各自的修改.
現在,模擬一個你的小夥伴,可以在另一臺電腦(註意要把SSH Key添加到GitHub)或者同一臺電腦的另一個目錄下克隆:
$ git clone [email protected]:triaquae/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 16 (delta 0), reused 10 (delta 0), pack-reused 0
Receiving objects: 100% (16/16), done.
Checking connectivity... done.
當你的小夥伴從遠程庫clone時,默認情況下,你的小夥伴只能看到本地的master分支。不信可以用git branch命令看看:
$ git branch
* master
現在,你的小夥伴要在dev分支上開發,就必須創建遠程origin的dev分支到本地,於是他用這個命令創建本地dev分支:
$ git checkout -b dev origin/dev
現在,他就可以在dev上繼續修改,然後,時不時地把dev分支push到遠程:
$ git add .
$ git commit -m "small updates"
 
[dev f1b762e] small updates
 2 files changed, 5 insertions(+), 1 deletion(-)
Alexs-MacBook-Pro:gitskills alex$ git push origin dev
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 438 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To [email protected]:triaquae/gitskills.git
   33ec6b4..f1b762e  dev -> dev
你的小夥伴已經向origin/dev分支推送了他的提交,而碰巧你也對同樣的文件作了修改,並試圖推送:
$ git add .
$ git commit -m "add Dog class"
[dev 7e7b1bf] add Dog class
 2 files changed, 7 insertions(+)
 
 
$ git push origin dev
 
To [email protected]:triaquae/gitskills.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to '[email protected]:triaquae/gitskills.git'
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.
推送失敗,因為你的小夥伴的最新提交和你試圖推送的提交有沖突,解決辦法也很簡單,Git已經提示我們,先用git pull把最新的提交從origin/dev抓下來,然後,在本地合並,解決沖突,再推
$ git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From github.com:triaquae/gitskills
   33ec6b4..f1b762e  dev        -> origin/dev
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
 
    git pull <remote> <branch>
 
If you wish to set tracking information for this branch you can do so with:
 
    git branch --set-upstream-to=origin/<branch> dev
git pull也失敗了,原因是沒有指定本地dev分支與遠程origin/dev分支的鏈接,根據提示,設置dev和origin/dev的鏈接:
$ git branch --set-upstream-to=origin/dev dev
Branch dev set up to track remote branch dev from origin.
再pull:
$ git pull
Auto-merging hello.py
CONFLICT (content): Merge conflict in hello.py
Auto-merging branch_test.md
CONFLICT (content): Merge conflict in branch_test.md
Automatic merge failed; fix conflicts and then commit the result.
這回git pull成功,但是合並有沖突,需要手動解決,解決的方法和分支管理中的解決沖突完全一樣。解決後,提交,再push:  
$ git add .
$ git commit -m "merge & fix hello.py"
[dev 93e28e3] merge & fix hello.py
 
$ git push origin dev
 
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 819 bytes | 0 bytes/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To [email protected]:triaquae/gitskills.git
   f1b762e..93e28e3  dev -> dev
因此,多人協作的工作模式通常是這樣:
首先,可以試圖用git push origin branch-name推送自己的修改;
    如果推送失敗,則因為遠程分支比你的本地更新,需要先用git pull試圖合並;
    如果合並有沖突,則解決沖突,並在本地提交;
    沒有沖突或者解決掉沖突後,再用git push origin branch-name推送就能成功!
如果git pull提示“no tracking information”,則說明本地分支和遠程分支的鏈接關系沒有創建.
    執行命令: git branch --set-upstream branch-name origin/branch-name。
這就是多人協作的工作模式,一旦熟悉了,就非常簡單.

12.GIT多人協作