二級(jí)域名指向 獨(dú)立網(wǎng)站營銷型企業(yè)網(wǎng)站
一、多人協(xié)作二
1.1多人協(xié)作
一般情況下,如果有多需求需要多人同時(shí)進(jìn)行開發(fā),是不會(huì)在一個(gè)分支上進(jìn)行多人開發(fā),而是一個(gè)需求或一個(gè)功能點(diǎn)就要?jiǎng)?chuàng)建一個(gè)feature 分支。
現(xiàn)在同時(shí)有兩個(gè)需求需要你和你的小伙伴進(jìn)行開發(fā),那么你們倆便可以各自創(chuàng)建一個(gè)分支來完成自己的工作。在上個(gè)部分我們已經(jīng)了解了可以從碼云上直接創(chuàng)建遠(yuǎn)程分支,其實(shí)在本地創(chuàng)建的分支也可以通過推送的方式發(fā)送到遠(yuǎn)端。在這個(gè)部分我們就來用一下這種方式。
- 對(duì)于你來說,可以進(jìn)行以下操作:
# 新增本地分支 feature-1 并切換
hyb@139-159-150-152:~/git_teaching$ git branch
dev
* master
hyb@139-159-150-152:~/git_teaching$ git checkout -b feature-1
Switched to a new branch 'feature-1'
# 新增需求內(nèi)容-創(chuàng)建function1文件
hyb@139-159-150-152:~/git_teaching$ vim function1
hyb@139-159-150-152:~/git_teaching$ cat function1
Done!
# 將 feature-1 分支推送到遠(yuǎn)端
hyb@139-159-150-152:~/git_teaching$ git add function1
hyb@139-159-150-152:~/git_teaching$ git commit -m"add function1"
[feature-1 12ed0db] add function1
1 file changed, 1 insertion(+)
create mode 100644 function1
hyb@139-159-150-152:~/git_teaching$ git push origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/hyb91/git_teaching/pull/new/hyb91:feature-1...hyb9
To gitee.com:hyb91/git_teaching.git
* [new branch] feature-1 -> feature-1
- 對(duì)于小伙伴來說,可以進(jìn)行以下操作:
此時(shí),在本地,你看不見他新建的文檔,他看不見你新建的文檔。并且推送各自的分支時(shí),并沒有任何沖突,你倆互不影響,用起來很舒服!!?
再來看下遠(yuǎn)端碼云上此時(shí)的狀態(tài):
對(duì)于你的 feature-1 分支:?對(duì)于小伙伴的 feature-2 分支:?
正常情況下,你倆就可以在自己的分支上進(jìn)行專業(yè)的開發(fā)了!
但天有不測風(fēng)云,你的小伙伴突然生病了,但需求還沒開發(fā)完,需要你幫他繼續(xù)開發(fā),于是他便把
feature-2 分支名告訴你了。這時(shí)你就需要在自己的機(jī)器上切換到 feature-2 分支幫忙繼續(xù)開發(fā),要做的操作如下:?
# 必須先拉取遠(yuǎn)端倉庫內(nèi)容
hyb@139-159-150-152:~/git_teaching$ git pull
...
From gitee.com:hyb91/git_teaching
305f78a..72c5345 dev -> origin/dev
* [new branch] feature-2 -> origin/feature-2
...
# 可以看到遠(yuǎn)程已經(jīng)有了feature-2
hyb@139-159-150-152:~/git_teaching$ git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
# 切換到feature-2分支上,可以和遠(yuǎn)程的feature-2分支關(guān)聯(lián)起來,
# 否則將來只使用 git push 推送內(nèi)容會(huì)失敗
hyb@139-159-150-152:~/git_teaching$ git checkout -b feature-2 origin/feature-2
Branch 'feature-2' set up to track remote branch 'feature-2' from 'origin'.
Switched to a new branch 'feature-2'
hyb@139-159-150-152:~/git_teaching$ ls
a.so b.ini file.txt function2 README.en.md README.md
切換成功后,便可以看見 feature-2 分支中的 function2 文件了,接著就可以幫小伙伴進(jìn)行開發(fā):
# 繼續(xù)開發(fā)
hyb@139-159-150-152:~/git_teaching$ vim function2
hyb@139-159-150-152:~/git_teaching$ cat function2
Done!
Help done!
# 推送內(nèi)容
hyb@139-159-150-152:~/git_teaching$ git add function2
hyb@139-159-150-152:~/git_teaching$ git commit -m"modify function2"
[feature-2 1079ae7] modify function2
1 file changed, 2 insertions(+), 1 deletion(-)
hyb@139-159-150-152:~/git_teaching$ git push origin feature-2
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 262.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
e1233f1..1079ae7 feature-2 -> feature-2
查看遠(yuǎn)程狀態(tài),推送成功了:
這時(shí),你的小伙伴已經(jīng)修養(yǎng)的差不多,可以繼續(xù)進(jìn)行自己的開發(fā)工作,那么他首先要獲取到你幫他開發(fā)的內(nèi)容,然后接著你的代碼繼續(xù)開發(fā)?;蛘吣阋呀?jīng)幫他開發(fā)完了,那他也需要在自己的電腦上看看你幫他寫的代碼:?
Pull 無效的原因是小伙伴沒有指定本地 feature-2 分支與遠(yuǎn)程 origin/feature-2 分支的鏈接,根據(jù)提
示,設(shè)置feature-2和origin/feature-2的鏈接即可:
?
目前,小伙伴的本地代碼和遠(yuǎn)端保持嚴(yán)格一致。你和你的小伙伴可以繼續(xù)在不同的分支下進(jìn)行協(xié)同開發(fā)了。各自功能開發(fā)完畢后,不要忘記我們需要將代碼合并到master中才算真正意義上的開發(fā)完畢。由于你的小伙伴率先開發(fā)完畢,于是開始 merge :?
此時(shí)遠(yuǎn)程倉庫的狀態(tài):?
當(dāng)你的小伙伴將其代碼merge 到master 后,這是你也開發(fā)完成了,也需要進(jìn)行 merge 到
master 操作,于是你:
# 切換至 master分支, pull 一下,保證本地的master是最新內(nèi)容。
# 合并前這么做是一個(gè)好習(xí)慣
hyb@139-159-150-152:~/git_teaching$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
hyb@139-159-150-152:~/git_teaching$ git pull
From gitee.com:hyb91/git_teaching
72c5345..29006bd master -> origin/master
Updating 72c5345..29006bd
Fast-forward
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
# 切換至 feature-1 分支, 合并 master 分支
# 這么做是因?yàn)槿绻袥_突,可以在feature-1分支上進(jìn)行處理,而不是在在master上解決沖突。
# 這么做是一個(gè)好習(xí)慣
hyb@139-159-150-152:~/git_teaching$ git checkout feature-1
Switched to branch 'feature-1'
Your branch is up to date with 'origin/feature-1'.
hyb@139-159-150-152:~/git_teaching$ git merge master
Merge made by the 'recursive' strategy.
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
hyb@139-159-150-152:~/git_teaching$ ls
a.so b.ini file.txt function1 function2 README.en.md README.md
# 1、由于feature-1分支已經(jīng)merge進(jìn)來了新內(nèi)容,為了保證遠(yuǎn)程分支最新,所以最好push一下。
# 2、要 push 的另一個(gè)原因是因?yàn)樵趯?shí)際的開發(fā)中,master的merge操作一般不是由我們自己在本地進(jìn)
# 其他人員或某些平臺(tái)merge時(shí),操作的肯定是遠(yuǎn)程分支,所以就要保證遠(yuǎn)程分支的最新。
# 3、如果 merge 出現(xiàn)沖突,不要忘記需要commit才可以push!!
hyb@139-159-150-152:~/git_teaching$ git status
On branch feature-1
Your branch is ahead of 'origin/feature-1' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
hyb@139-159-150-152:~/git_teaching$ git push origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 299 bytes | 299.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
ea75a35..4b4c3d4 feature-1 -> feature-1
# 切換至 master 分支,合并 feature-1 分支
hyb@139-159-150-152:~/git_teaching$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
hyb@139-159-150-152:~/git_teaching$ git merge feature-1
Updating 29006bd..4b4c3d4
Fast-forward
function1 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 function1
hyb@139-159-150-152:~/git_teaching$ ls
a.so b.ini file.txt function1 function2 README.en.md README.md
# 將 master 分支推送至遠(yuǎn)端
hyb@139-159-150-152:~/git_teaching$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
hyb@139-159-150-152:~/git_teaching$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
29006bd..4b4c3d4 master -> master
hyb@139-159-150-152:~/git_teaching$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
?此時(shí)遠(yuǎn)程倉庫的狀態(tài):
此時(shí), feature-1 和feature-2 分支對(duì)于我們來說就沒用了, 那么我們可以直接在遠(yuǎn)程倉庫中
將dev分支刪除掉:
1.2遠(yuǎn)程分支刪除后,本地 git branch -a 依然能看到的解決辦法
當(dāng)前我們已經(jīng)刪除了遠(yuǎn)程的幾個(gè)分支,使用 git branch -a 命令可以查看所有本地分支和遠(yuǎn)程分
支,但發(fā)現(xiàn)很多在遠(yuǎn)程倉庫已經(jīng)刪除的分支在本地依然可以看到。例如:
hyb@139-159-150-152:~/git_teaching$ git pull
Already up to date.
hyb@139-159-150-152:~/git_teaching$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
?使用命令 git remote show origin ,可以查看remote地址,遠(yuǎn)程分支,還有本地分支與之相
對(duì)應(yīng)關(guān)系等信息。
hyb@139-159-150-152:~/git_teaching$ git remote show origin
* remote origin
Fetch URL: git@gitee.com:hyb91/git_teaching.git
Push URL: git@gitee.com:hyb91/git_teaching.git
HEAD branch: master
Remote branches:
master tracked
refs/remotes/origin/dev stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-1 stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-2 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev merges with remote dev
feature-1 merges with remote feature-1
feature-2 merges with remote feature-2
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
此時(shí)我們可以看到那些遠(yuǎn)程倉庫已經(jīng)不存在的分支,根據(jù)提示,使用 git remote prune
origin 命令:
hyb@139-159-150-152:~/git_teaching$ git remote prune origin
Pruning origin
URL: git@gitee.com:hyb91/git_teaching.git
* [pruned] origin/dev
* [pruned] origin/feature-1
* [pruned] origin/feature-2
hyb@139-159-150-152:~/git_teaching$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
這樣就刪除了那些遠(yuǎn)程倉庫不存在的分支。