GitHub分支管理 - 多人協作
1. 多人協作
當你從遠端倉庫克隆時,實際上Git自動把本地的master
分支和遠端的master
分支對應起來了,並且,遠端倉庫的預設名稱是origin
。
要檢視遠端庫的資訊,用git remote
:
1 [root@mini05 linuxmi]# git remote
2 origin
3 [root@mini05 linuxmi]# git remote -v # 詳細資訊
4 origin [email protected]:linuxidc/linuxmi.git (fetch)
5 origin [email protected]:linuxidc/linuxmi.git (push)
上面顯示了可以抓取和推送的origin
的地址。如果沒有推送許可權,就看不到push的地址。
2. 推送分支
推送分支,就是把該分支上的所有本地提交推送到遠端庫。推送時,要指定本地分支,這樣,Git就會把該分支推送到遠端庫對應的遠端分支上:
1 [root@mini05 linuxmi]# git push origin master
2 ………………
如果要推送其他分支,比如dev,就改成:
1 [root@mini05 linuxmi]# git push origin dev
2 ………………
但是,並不是一定要把本地分支往遠端推送,那麼,哪些分支需要推送,哪些不需要呢?
master dev
3. 抓取分支
3.1. 在mini04機器操作
多人協作時,大家都會往master
和dev
分支上推送各自的修改。
現在,模擬一個你的小夥伴,可以在另一臺電腦(注意要把SSH Key新增到GitHub)或者同一臺電腦的另一個目錄下克隆:
1 [root@mini04 git_repository]# git clone [email protected]:linuxidc/linuxmi.git
2 ………………
當你的小夥伴從遠端庫clone時,預設情況下,你的小夥伴只能看到本地的master分支。不信可以用git branch命令看看:
1 [root@mini04 linuxmi]# git branch
2 * master
現在,你的小夥伴要在dev分支上開發,就必須建立遠端origin的dev分支到本地,於是他用這個命令建立本地dev分支:
1 [root@mini04 linuxmi]# git checkout -b dev origin/dev
現在,他就可以在dev上繼續修改,然後,時不時地把dev分支push到遠端:
[root@mini04 linuxmi]# cat env.txt # 新加的檔案
# env.info
[root@mini04 linuxmi]# git add env.txt
[root@mini04 linuxmi]# git commit -m "add env"
[dev 645aae1] add env
1 file changed, 1 insertion(+)
create mode 100644 env.txt
[root@mini04 linuxmi]# git push origin dev # 推送到遠端倉庫
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To [email protected]:linuxidc/linuxmi.git
f168250..645aae1 dev -> dev
3.2. 在mini05機器操作
你的小夥伴已經向origin/dev
分支推送了他的提交,而碰巧你也對同樣的檔案作了修改,並試圖推送:
[root@mini05 linuxmi]# cat env.txt # env project=test [root@mini05 linuxmi]# git add env.txt [root@mini05 linuxmi]# git commit -m "add new env" [dev 1cb2e9c] add new env 1 file changed, 2 insertions(+) create mode 100644 env.txt [root@mini05 linuxmi]# git push origin dev # 推送到遠端 To [email protected]:linuxidc/linuxmi.git ! [rejected] dev -> dev (fetch first) error: failed to push some refs to '[email protected]:linuxidc/linuxmi.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 merge the remote changes (e.g., hint: 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
推送失敗,因為你的小夥伴的最新提交和你試圖推送的提交有衝突,解決辦法也很簡單,Git已經提示我們,先用git pull
把最新的提交從origin/dev
抓下來,然後,在本地合併,解決衝突,再推送:
[root@mini05 linuxmi]# git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:linuxidc/linuxmi f168250..645aae1 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
的連結:
1 [root@mini05 linuxmi]# git branch --set-upstream-to=origin/dev dev
2 Branch dev set up to track remote branch dev from origin.
再次pull:
1 [root@mini05 linuxmi]# git pull
2 Auto-merging env.txt
3 CONFLICT (add/add): Merge conflict in env.txt
4 Automatic merge failed; fix conflicts and then commit the result.
這回git pull
成功,但是合併有衝突,需要手動解決,解決的方法和分支管理中的解決衝突完全一樣。解決後,提交,再push:
[root@mini05 linuxmi]# cat env.txt # 手動合併保留的資訊
# env.info
project=test
[root@mini05 linuxmi]# git add env.txt # 重新加入暫存區
[root@mini05 linuxmi]# git commit -m "fix env conflict" # 提交
[dev 67223d3] fix env conflict
[root@mini05 linuxmi]# git push origin dev # 推動到遠端倉庫
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 576 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To [email protected]:linuxidc/linuxmi.git
645aae1..67223d3 dev -> dev
3.3. 多人工作模式
因此,多人協作的工作模式通常是這樣:
git push origin <branch-name> git pull git push origin <branch-name>
說明:
如果git pull
提示no tracking information
,則說明本地分支和遠端分支的連結關係沒有建立,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>
。
這就是多人協作的工作模式,一旦熟悉了,就非常簡單。
4. 小結
-
檢視遠端庫資訊,使用
git remote -v
; - 本地新建的分支如果不推送到遠端,對其他人就是不可見的;
-
從本地推送分支,使用
git push origin branch-name
,如果推送失敗,先用git pull
抓取遠端的新提交; -
在本地建立和遠端分支對應的分支,使用
git checkout -b branch-name origin/branch-name
,本地和遠端分支的名稱最好一致; -
建立本地分支和遠端分支的關聯,使用
git branch --set-upstream branch-name origin/branch-name
; -
從遠端抓取分支,使用
git pull
,如果有衝突,要先處理衝突。
Linux公社的RSS地址 :ofollow,noindex" target="_blank">https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-10/154583.htm