本節內容:git
查看遠程庫信息,使用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,若是有衝突,要先處理衝突。
當你從遠程倉庫克隆時,實際上Git自動把本地的master
分支和遠程的master
分支對應起來了,而且,遠程倉庫的默認名稱是origin
。github
要查看遠程庫的信息,用git remote
:學習
lwenhaodeMacBook-Pro:TestGit lwenhao$ git remote origin lwenhaodeMacBook-Pro:TestGit lwenhao$
或者用git remote -v
顯示更詳細的信息:fetch
lwenhaodeMacBook-Pro:TestGit lwenhao$ git remote -v origin https://github.com/liuwenhaoCN/TestGit.git (fetch) origin https://github.com/liuwenhaoCN/TestGit.git (push) lwenhaodeMacBook-Pro:TestGit lwenhao$
上面顯示了能夠抓取和推送的origin
的地址。若是沒有推送權限,就看不到push的地址。this
推送分支,就是把該分支上的全部本地提交推送到遠程庫。推送時,要指定本地分支,這樣Git就會把該分支推送到遠程庫對應的遠程分支上:.net
git push origin master
若是要推送其餘分支,好比dev
,就改爲:code
lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin dev Total 0 (delta 0), reused 0 (delta 0) remote: This repository moved. Please use the new location: remote: https://github.com/lwenhaoCN/TestGit.git remote: remote: Create a pull request for 'dev' on GitHub by visiting: remote: https://github.com/lwenhaoCN/TestGit/pull/new/dev remote: To https://github.com/liuwenhaoCN/TestGit.git * [new branch] dev -> dev lwenhaodeMacBook-Pro:TestGit lwenhao$
可是,並非必定要把本地分支往遠程推送,那麼,哪些分支須要推送,哪些不須要呢?orm
master
分支是主分支,所以要時刻與遠程同步;dev
分支是開發分支,團隊全部成員都須要在上面工做,因此也須要與遠程同步;總之,就是在Git中,分支徹底能夠在本地本身藏着玩,是否推送,視你的心情而定!blog
多人協做時,你們都會往master
和dev
分支上推送各自的修改。ci
如今,模擬一個你的小夥伴,能夠在另外一臺電腦(注意要把SSH Key添加到GitHub)或者同一臺電腦的另外一個目錄下克隆:新建Test
文件夾,把TestGit
克隆到該文件夾(這個文件夾模擬你的小夥伴)。
lwenhaodeMacBook-Pro:Test lwenhao$ git clone 'https://github.com/lwenhaoCN/TestGit.git' Cloning into 'TestGit'... remote: Enumerating objects: 23, done. remote: Counting objects: 100% (23/23), done. remote: Compressing objects: 100% (10/10), done. remote: Total 23 (delta 3), reused 20 (delta 3), pack-reused 0 Unpacking objects: 100% (23/23), done. lwenhaodeMacBook-Pro:Test lwenhao$
當你的小夥伴從遠程庫clone時,默認狀況下,你的小夥伴只能看到本地的master
分支。使用git branch
命令查看:
lwenhaodeMacBook-Pro:Test lwenhao$ cd TestGit/ lwenhaodeMacBook-Pro:TestGit lwenhao$ git branch * master lwenhaodeMacBook-Pro:TestGit lwenhao$
如今,你的小夥伴要在dev
分支上開發,就必須建立遠程origin
的dev
分支到本地,因而他用這個命令建立本地dev
分支:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout -b dev origin/dev Branch 'dev' set up to track remote branch 'dev' from 'origin'. Switched to a new branch 'dev' lwenhaodeMacBook-Pro:TestGit lwenhao$
而且你的小夥伴修改README.md
文件內容:
lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit 建立一個"dev"分支,我來操做。 學習分支管理策略。 dev、dev、dev lwenhaodeMacBook-Pro:TestGit lwenhao$
他就能夠在dev
上繼續修改,而後時不時地把dev
分支push
到遠程:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "add dev" [dev 0f59233] add dev 1 file changed, 2 insertions(+) lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin dev Counting objects: 3, done. Delta compression using up to 12 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 267 bytes | 267.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/lwenhaoCN/TestGit.git 8faa495..0f59233 dev -> dev lwenhaodeMacBook-Pro:TestGit lwenhao$
你的小夥伴已經向origin/dev
分支推送了他的提交,而碰巧你也對一樣的文件做了修改,並試圖推送:
如今返回到你原來的TestGit
中,而且修改README.md
內容:
lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit 建立一個"dev"分支,我來操做。 學習分支管理策略。 我本身寫的。 lwenhaodeMacBook-Pro:TestGit lwenhao$
你本身提交dev
分支下的README.md
lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "add my" [dev 29bc6cb] add my 1 file changed, 2 insertions(+) lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin dev To https://github.com/liuwenhaoCN/TestGit.git ! [rejected] dev -> dev (fetch first) error: failed to push some refs to 'https://github.com/liuwenhaoCN/TestGit.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. lwenhaodeMacBook-Pro:TestGit lwenhao$
推送失敗,由於你的小夥伴的最新提交和你試圖推送的提交有衝突,解決辦法也很簡單,Git已經提示咱們,先用git pull
把最新的提交從origin/dev
抓下來,而後在本地合併,解決衝突,再推送:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git pull remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), 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 https://github.com/liuwenhaoCN/TestGit 8faa495..0f59233 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 lwenhaodeMacBook-Pro:TestGit lwenhao$
git pull
也失敗了,緣由是沒有指定本地dev
分支與遠程origin/dev
分支的連接,根據提示,設置dev
和origin/dev
的連接:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git branch --set-upstream-to=origin/dev dev Branch 'dev' set up to track remote branch 'dev' from 'origin'. lwenhaodeMacBook-Pro:TestGit lwenhao$
在使用git pull
:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git pull Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result. lwenhaodeMacBook-Pro:TestGit lwenhao$
這回git pull
成功,可是合併有衝突,須要手動解決,解決的方法和04 分支管理 —— 解決衝突的徹底同樣。解決後,提交,再push:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "fix README.md conflict" [dev cd4d07d] fix README.md conflict lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin dev Counting objects: 6, done. Delta compression using up to 12 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 524 bytes | 524.00 KiB/s, done. Total 6 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 1 local object. remote: This repository moved. Please use the new location: remote: https://github.com/lwenhaoCN/TestGit.git To https://github.com/liuwenhaoCN/TestGit.git 0f59233..cd4d07d dev -> dev lwenhaodeMacBook-Pro:TestGit lwenhao$
所以,多人協做的工做模式一般是這樣:
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>
。