備注:
本文參考于廖雪峰老師的博客Git教程衔峰。依照其博客進(jìn)行學(xué)習(xí)和記錄,感謝其無私分享趋距,也歡迎各位查看原文挂绰。
知識點(diǎn)
強(qiáng)制刪除未合并的分支宛徊,
git branch -D <branch-name>
歧杏,將丟失分支的修改git remote
和git remote -v
顯示遠(yuǎn)程倉庫信息git push origin branch-name
將本地分支推送到遠(yuǎn)程陪每。默認(rèn)clone遠(yuǎn)程庫后影晓,只能看到master分支,其他分支需要設(shè)定跟蹤檩禾,
git checkout -b dev origin/dev
dev分支設(shè)置為跟蹤來自origin
的遠(yuǎn)程分支dev
挂签。git branch --set-upstream-to=origin/<branch> branch
創(chuàng)建本地分支與遠(yuǎn)程分支的跟蹤,用于git push
和git pull
git pull <remote> <branch>
拉取指定的遠(yuǎn)程分支
Feature分支
軟件開發(fā)中盼产,總有無窮無盡的新的功能要不斷添加進(jìn)來饵婆。
添加一個(gè)新功能時(shí),你肯定不希望因?yàn)橐恍?shí)驗(yàn)性質(zhì)的代碼戏售,把主分支搞亂了侨核,所以草穆,每添加一個(gè)新功能,最好新建一個(gè)feature
分支搓译,在上面開發(fā)悲柱,完成后,合并些己,最后豌鸡,刪除該feature
分支。
比如你接到了一個(gè)新任務(wù):開發(fā)代號為Vulcan
的新功能段标,該功能計(jì)劃用于下一代星際飛船涯冠。
于是準(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>
即這個(gè)分支需要就地銷毀:
$ 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
地址,具有拉取和推送的兩個(gè)權(quán)限
推送分支
- 推送分支拆讯,是將該分支上的所有本地提交推送到遠(yuǎn)程庫脂男。推送時(shí)需要指定本地分支,Git將把該分支推送到遠(yuǎn)程對應(yīng)的分支上:
$ git push origin master
上面將會把本地master推送到origin master种呐。本地其他分支不會被推送
-
git push origin dev
宰翅,推送其他分支,如果遠(yuǎn)程沒有則創(chuàng)建(dev
)分支并推送
遠(yuǎn)程分支推送建議
master
分支是主分支爽室,因此要時(shí)刻與遠(yuǎn)程同步
dev
分支是開發(fā)分支汁讼,團(tuán)隊(duì)所有成員都需要在上面工作,所以也需要與遠(yuǎn)程同步
bug
分支只用于在本地修復(fù)bug,就沒必要推到遠(yuǎn)程了嘿架,除非老板要看看你每周到底修復(fù)了幾個(gè)bug
feature
分支是否推到遠(yuǎn)程瓶珊,取決于你是否和你的小伙伴合作在上面開發(fā)
克隆(clone)遠(yuǎn)程倉庫
在多人協(xié)作中,比如耸彪,模擬另外一小伙伴(另一臺電腦或另一個(gè)目錄下)伞芹,從遠(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蜀肘。
切換到一個(gè)新分支 'dev'
- 新分支已與遠(yuǎn)程倉庫保持同步。
現(xiàn)在另一個(gè)伙伴修改dev
分支并push
到遠(yuǎn)程
$ git push origin dev
Username for 'https://github.com': findmoon
Password for 'https://findmoon@github.com':
對象計(jì)數(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í)指定分支或設(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)榱硗?提示:一個(gè)倉庫已向該引用進(jìn)行了推送。再次推送前薄腻,您可能需要先整合遠(yuǎn)程變更
提示:(如 'git pull ...')收捣。
提示:詳見 'git push --help' 中的 'Note about fast-forwards' 小節(jié)。
提示無法推送庵楷,更新被拒絕罢艾,Git提示,推送需要先整合變更
- 遠(yuǎn)程倉庫有變更時(shí)尽纽,再次推送需要先整合變更咐蚯,使用
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)前分支沒有跟蹤信息。
請指定您要合并哪一個(gè)分支弄贿。
詳見 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í)文件沖突
上面設(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é)果脚线。
此時(shí)可以git pull
搁胆,但是合并有沖突。解決沖突和本地分支管理中的沖突辦法一樣,
- 手動修改
git pull
時(shí)的合并沖突渠旁,然后提交攀例,最后再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
對象計(jì)數(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
拉取和推送完成粤铭。
另外一個(gè)小伙伴直接git pull
,保持與遠(yuǎn)程庫的更新,此時(shí)會將遠(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(-)
多人協(xié)作的工作模式建議
- 首先梆惯,可以試圖用
git push origin branch-name
推送自己的修改- 如果推送失敗,則因?yàn)檫h(yuǎn)程分支比你的本地更新吗垮,需要先用
git pull
試圖合并- 如果合并有沖突垛吗,則解決沖突,并在本地提交
- 沒有沖突或者解決掉沖突后烁登,再用
git push origin branch-name
推送就能成功