當(dāng)你從遠(yuǎn)程倉庫克隆時,實際上Git自動把本地的master分支和遠(yuǎn)程的master分支對應(yīng)起來了,并且,遠(yuǎn)程倉庫的默認(rèn)名稱是origin。
要查看遠(yuǎn)程庫的信息,用git remote:
$ git remote
origin
或者,用git remote -v顯示更詳細(xì)的信息:
$ git remote -v
origin [email protected]:michaelliao/learngit.git (fetch)
origin [email protected]:michaelliao/learngit.git (push)
上面顯示了可以抓取和推送的origin的地址。如果沒有推送權(quán)限,就看不到push的地址。
推送分支,就是把該分支上的所有本地提交推送到遠(yuǎn)程庫。推送時,要指定本地分支,這樣,Git就會把該分支推送到遠(yuǎn)程庫對應(yīng)的遠(yuǎn)程分支上:
$ git push origin master
如果要推送其他分支,比如dev,就改成:
$ git push origin dev
但是,并不是一定要把本地分支往遠(yuǎn)程推送,那么,哪些分支需要推送,哪些不需要呢?
① master分支是主分支,因此要時刻與遠(yuǎn)程同步;
② dev分支是開發(fā)分支,團隊所有成員都需要在上面工作,所以也需要與遠(yuǎn)程同步;
③ bug分支只用于在本地修復(fù)bug,就沒必要推到遠(yuǎn)程了,除非老板要看看你每周到底修復(fù)了幾個bug;
④ feature分支是否推到遠(yuǎn)程,取決于你是否和你的小伙伴合作在上面開發(fā)。
總之,就是在Git中,分支完全可以在本地自己藏著玩,是否推送,視你的心情而定!
多人協(xié)作時,大家都會往master和dev分支上推送各自的修改。
現(xiàn)在,模擬一個你的小伙伴,可以在另一臺電腦(注意要把SSH Key添加到GitHub)或者同一臺電腦的另一個目錄下克隆:
$ git clone [email protected]:michaelliao/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 40, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 40 (delta 14), reused 40 (delta 14), pack-reused 0
Receiving objects: 100% (40/40), done.
Resolving deltas: 100% (14/14), done.
當(dāng)你的小伙伴從遠(yuǎn)程庫clone時,默認(rèn)情況下,你的小伙伴只能看到本地的master分支。不信可以用git branch命令看看:
$ git branch
* master
現(xiàn)在,你的小伙伴要在dev分支上開發(fā),就必須創(chuàng)建遠(yuǎn)程origin的dev分支到本地,于是他用這個命令創(chuàng)建本地dev分支:
$ git checkout -b dev origin/dev
現(xiàn)在,他就可以在dev上繼續(xù)修改,然后,時不時地把dev分支push到遠(yuǎn)程:
$ git add env.txt
$ git commit -m "add env"
[dev 7a5e5dd] add env
1 file changed, 1 insertion(+)
create mode 100644 env.txt
$ git push origin dev
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
f52c633..7a5e5dd dev -> dev
你的小伙伴已經(jīng)向origin/dev分支推送了他的提交,而碰巧你也對同樣的文件作了修改,并試圖推送:
$ cat env.txt
env
$ git add env.txt
$ git commit -m "add new env"
[dev 7bd91f1] add new env
1 file changed, 1 insertion(+)
create mode 100644 env.txt
$ git push origin dev
To github.com:michaelliao/learngit.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to '[email protected]:michaelliao/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate 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已經(jīng)提示我們,先用git pull把最新的提交從origin/dev抓下來,然后,在本地合并,解決沖突,再推送:
$ git pull
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分支與遠(yuǎn)程origin/dev分支的鏈接,根據(jù)提示,設(shè)置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 env.txt
CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.
這回git pull成功,但是合并有沖突,需要手動解決,解決的方法和分支管理中的解決沖突完全一樣。解決后,提交,再push:
$ git commit -m "fix env conflict"
[dev 57c53ab] fix env conflict
$ git push origin dev
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
7a5e5dd..57c53ab dev -> dev
因此,多人協(xié)作的工作模式通常是這樣:
① 首先,可以試圖用git push origin 推送自己的修改;
② 如果推送失敗,則因為遠(yuǎn)程分支比你的本地更新,需要先用git pull試圖合并;
③ 如果合并有沖突,則解決沖突,并在本地提交;
④ 沒有沖突或者解決掉沖突后,再用git push origin 推送就能成功!
如果git pull提示no tracking information,則說明本地分支和遠(yuǎn)程分支的鏈接關(guān)系沒有創(chuàng)建,用命令git branch --set-upstream-to origin/。
這就是多人協(xié)作的工作模式,一旦熟悉了,就非常簡單。
① 查看遠(yuǎn)程庫信息,使用git remote -v;
② 本地新建的分支如果不推送到遠(yuǎn)程,對其他人就是不可見的;
③ 從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠(yuǎn)程的新提交;
④ 在本地創(chuàng)建和遠(yuǎn)程分支對應(yīng)的分支,使用git checkout -b branch-name origin/branch-name,本地和遠(yuǎn)程分支的名稱最好一致;
⑤ 建立本地分支和遠(yuǎn)程分支的關(guān)聯(lián),使用git branch --set-upstream branch-name origin/branch-name;
⑥ 從遠(yuǎn)程抓取分支,使用git pull,如果有沖突,要先處理沖突。