git的分支遠(yuǎn)程連接和遠(yuǎn)程分支的拉取推送及沖突處理

Feature分支

軟件開發(fā)中,總有無窮無盡的新的功能要不斷添加進(jìn)來。

添加一個新功能時,你肯定不希望因?yàn)橐恍?shí)驗(yàn)性質(zhì)的代碼,把主分支搞亂了,所以,每添加一個新功能,最好新建一個feature分支,在上面開發(fā),完成后,合并,最后,刪除該feature分支。

比如你接到了一個新任務(wù):開發(fā)代號為Vulcan的新功能,該功能計劃用于下一代星際飛船。

于是準(zhǔn)備開發(fā):

$ git checkout -b feature-vulcan

Switched to a new branch 'feature-vulcan'

5分鐘后,開發(fā)完畢:

$ git add vulcan.py

$ git status

# On branch feature-vulcan

# Changes to be committed:

#? (use "git reset HEAD <file>..." to unstage)

#

#? ? ? new file:? vulcan.py

#

$ git commit -m "add feature vulcan"

[feature-vulcan 756d4af] add feature vulcan

1 file changed, 2 insertions(+)

create mode 100644 vulcan.py

切回dev,準(zhǔn)備合并:

$ git checkout dev

一切順利的話,feature分支和bug分支是類似的,合并,然后刪除。

但是,因?yàn)槟承┰颍摴δ苄枰∠?/p>

即這個分支需要就地銷毀:

$ git branch -d feature-vulcan

error: The branch 'feature-vulcan' is not fully merged.

If you are sure you want to delete it, run 'git branch -D feature-vulcan'.

銷毀失敗。Git提示,feature-vulcan分支還沒有被合并,如果刪除,將丟失掉修改,如果要強(qiáng)行刪除,需要使用命令git branch -D feature-vulcan。

現(xiàn)在我們強(qiáng)行刪除:

$ git branch -D feature-vulcan

Deleted branch feature-vulcan (was 756d4af).

多人協(xié)作

從遠(yuǎn)程倉庫克隆,Git自動把本地master分支和遠(yuǎn)程master分支對應(yīng)起來,遠(yuǎn)程倉庫默認(rèn)名稱是origin

查看遠(yuǎn)程倉庫信息,git remote

$ git remote

origin

git remote -v顯示遠(yuǎn)程倉庫更詳細(xì)信息

$ git remote -v

origin git@github.com:findmoon/newrepo.git (fetch)

origin git@github.com:findmoon/newrepo.git (push)

如上為拉取和推送的origin地址,具有拉取和推送的兩個權(quán)限

推送分支

推送分支,是將該分支上的所有本地提交推送到遠(yuǎn)程庫。推送時需要指定本地分支,Git將把該分支推送到遠(yuǎn)程對應(yīng)的分支上:

$ git push origin master

上面將會把本地master推送到origin master。本地其他分支不會被推送

git push origin dev,推送其他分支,如果遠(yuǎn)程沒有則創(chuàng)建(dev)分支并推送

遠(yuǎn)程分支推送建議

master分支是主分支,因此要時刻與遠(yuǎn)程同步

dev分支是開發(fā)分支,團(tuán)隊所有成員都需要在上面工作,所以也需要與遠(yuǎn)程同步

bug分支只用于在本地修復(fù)bug,就沒必要推到遠(yuǎn)程了,除非老板要看看你每周到底修復(fù)了幾個bug

feature分支是否推到遠(yuǎn)程,取決于你是否和你的小伙伴合作在上面開發(fā)

克隆(clone)遠(yuǎn)程倉庫

在多人協(xié)作中,比如,模擬另外一小伙伴(另一臺電腦或另一個目錄下),從遠(yuǎn)程克隆倉庫

clone倉庫

$ git clone https://github.com/findmoon/newrepo.git

正克隆到 'newrepo'...

remote: Counting objects: 55, done.

remote: Compressing objects: 100% (33/33), done.

remote: Total 55 (delta 20), reused 54 (delta 19), pack-reused 0

展開對象中: 100% (55/55), 完成.

檢查連接... 完成。

分支的推送和沖突處理

關(guān)聯(lián)本地分支和遠(yuǎn)程分支

默認(rèn)情況下,從遠(yuǎn)程庫clone,只能看到遠(yuǎn)程master分支在本地的master分支

$ cd newrepo/

$ git branch

* master

要想在dev分支上開發(fā),需要創(chuàng)建本地dev分支并設(shè)置為跟蹤遠(yuǎn)程origin的dev分支

$ git checkout -b dev origin/dev

分支 dev 設(shè)置為跟蹤來自 origin 的遠(yuǎn)程分支 dev。

切換到一個新分支 'dev'

新分支已與遠(yuǎn)程倉庫保持同步。

現(xiàn)在另一個伙伴修改dev分支并push到遠(yuǎn)程

$ git push origin dev

Username for 'https://github.com': findmoon

Password for 'https://findmoon@github.com':

對象計數(shù)中: 3, 完成.

Delta compression using up to 4 threads.

壓縮對象中: 100% (3/3), 完成.

寫入對象中: 100% (3/3), 319 bytes | 0 bytes/s, 完成.

Total 3 (delta 2), reused 0 (delta 0)

remote: Resolving deltas: 100% (2/2), completed with 2 local objects.

To https://github.com/findmoon/newrepo.git

? 09a36ec..5a15ca7? dev -> dev

新的本地倉庫第一次推送到遠(yuǎn)程。需要輸入github的用戶名和密碼

推送時指定分支或設(shè)置分支跟蹤

在小伙伴推送origin/dev之后,你也對相同文件做了修改,并推送

$ git push origin dev

To git@github.com:findmoon/newrepo.git

! [rejected]? ? ? ? dev -> dev (fetch first)

error: 無法推送一些引用到 'git@github.com:findmoon/newrepo.git'

提示:更新被拒絕,因?yàn)檫h(yuǎn)程倉庫包含您本地尚不存在的提交。這通常是因?yàn)榱硗?/p>

提示:一個倉庫已向該引用進(jìn)行了推送。再次推送前,您可能需要先整合遠(yuǎn)程變更

提示:(如 'git pull ...')。

提示:詳見 'git push --help' 中的 'Note about fast-forwards' 小節(jié)。

提示無法推送,更新被拒絕,Git提示,推送需要先整合變更

遠(yuǎn)程倉庫有變更時,再次推送需要先整合變更,使用git pull

使用git pull拉取遠(yuǎn)程最新的提交

$ git pull

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (1/1), done.

remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0

展開對象中: 100% (3/3), 完成.

來自 github.com:findmoon/newrepo

? 09a36ec..5a15ca7? dev? ? ? ? -> origin/dev

當(dāng)前分支沒有跟蹤信息。

請指定您要合并哪一個分支。

詳見 git-pull(1)。

? ? git pull <remote> <branch>

如果您想要為此分支創(chuàng)建跟蹤信息,您可以執(zhí)行:

? ? git branch --set-upstream-to=origin/<branch> dev

git pull失敗,原因是: git pull需要指定本地分支與遠(yuǎn)程origin分支的跟蹤,或者在git pull參數(shù)中指定遠(yuǎn)程分支

git branch --set-upstream-to=origin/<branch> branch創(chuàng)建本地分支與遠(yuǎn)程分支的跟蹤,用于git push和git pull

git pull <remote> <branch>指定拉取的遠(yuǎn)程分支

設(shè)置跟蹤遠(yuǎn)程分支

$ git branch --set-upstream-to=origin/dev dev

分支 dev 設(shè)置為跟蹤來自 origin 的遠(yuǎn)程分支 dev。

拉取分支時文件沖突

上面設(shè)置好跟蹤后重新pull拉取更新

$ git branch --set-upstream-to=origin/dev dev

分支 dev 設(shè)置為跟蹤來自 origin 的遠(yuǎn)程分支 dev。

$ git pull

自動合并 readme.txt

沖突(內(nèi)容):合并沖突于 readme.txt

自動合并失敗,修正沖突然后提交修正的結(jié)果。

此時可以git pull,但是合并有沖突。解決沖突和本地分支管理中的沖突辦法一樣,

手動修改git pull時的合并沖突,然后提交,最后再push

查看沖突文件

$ cat readme.txt

dev modify again commit on master

110

<<<<<<< HEAD

I modify readme on dev branch

=======

modify dev branch file on another people

>>>>>>> 5a15ca7c06dd2204fee3f4571e86b2bf64f6a83b

刪除沖突項(xiàng)后重新添加、提交和推送。

$ git add .

$ git commit -m"fixed remote conflict"

[dev 8a954f4] fixed remote conflict

$ git push origin dev

對象計數(shù)中: 6, 完成.

Delta compression using up to 4 threads.

壓縮對象中: 100% (6/6), 完成.

寫入對象中: 100% (6/6), 570 bytes | 0 bytes/s, 完成.

Total 6 (delta 4), reused 0 (delta 0)

remote: Resolving deltas: 100% (4/4), completed with 2 local objects.

To git@github.com:findmoon/newrepo.git

? 5a15ca7..8a954f4? dev -> dev

拉取和推送完成。

另外一個小伙伴直接git pull,保持與遠(yuǎn)程庫的更新,此時會將遠(yuǎn)程庫內(nèi)容合并到本地。

$ git pull origin dev

remote: Counting objects: 6, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0

展開對象中: 100% (6/6), 完成.

來自 https://github.com/findmoon/newrepo

* branch? ? ? ? ? ? dev? ? ? ? -> FETCH_HEAD

? 5a15ca7..8a954f4? dev? ? ? ? -> origin/dev

更新 5a15ca7..8a954f4

Fast-forward

readme.txt | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)

龍華大道1號http://www.kinghill.cn/LongHuaDaDao1Hao/index.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容