一、多人协作二
1.1多人协作
一般情况下,如果有多需求需要多人同时进行开发,是不会在一个分支上进行多人开发,而是一个需求或一个功能点就要创建一个feature 分支。
现在同时有两个需求需要你和你的小伙伴进行开发,那么你们俩便可以各自创建一个分支来完成自己的工作。在上个部分我们已经了解了可以从码云上直接创建远程分支,其实在本地创建的分支也可以通过推送的方式发送到远端。在这个部分我们就来用一下这种方式。
- 对于你来说,可以进行以下操作:
# 新增本地分支 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'
# 新增需求内容-创建function1文件
hyb@139-159-150-152:~/git_teaching$ vim function1
hyb@139-159-150-152:~/git_teaching$ cat function1
Done!
# 将 feature-1 分支推送到远端
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
- 对于小伙伴来说,可以进行以下操作:
此时,在本地,你看不见他新建的文档,他看不见你新建的文档。并且推送各自的分支时,并没有任何冲突,你俩互不影响,用起来很舒服!!
再来看下远端码云上此时的状态:
对于你的 feature-1 分支: 对于小伙伴的 feature-2 分支:
正常情况下,你俩就可以在自己的分支上进行专业的开发了!
但天有不测风云,你的小伙伴突然生病了,但需求还没开发完,需要你帮他继续开发,于是他便把
feature-2 分支名告诉你了。这时你就需要在自己的机器上切换到 feature-2 分支帮忙继续开发,要做的操作如下:
# 必须先拉取远端仓库内容
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
...
# 可以看到远程已经有了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分支上,可以和远程的feature-2分支关联起来,
# 否则将来只使用 git push 推送内容会失败
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 文件了,接着就可以帮小伙伴进行开发:
# 继续开发
hyb@139-159-150-152:~/git_teaching$ vim function2
hyb@139-159-150-152:~/git_teaching$ cat function2
Done!
Help done!
# 推送内容
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
查看远程状态,推送成功了:
这时,你的小伙伴已经修养的差不多,可以继续进行自己的开发工作,那么他首先要获取到你帮他开发的内容,然后接着你的代码继续开发。或者你已经帮他开发完了,那他也需要在自己的电脑上看看你帮他写的代码:
Pull 无效的原因是小伙伴没有指定本地 feature-2 分支与远程 origin/feature-2 分支的链接,根据提
示,设置feature-2和origin/feature-2的链接即可:
目前,小伙伴的本地代码和远端保持严格一致。你和你的小伙伴可以继续在不同的分支下进行协同开发了。各自功能开发完毕后,不要忘记我们需要将代码合并到master中才算真正意义上的开发完毕。由于你的小伙伴率先开发完毕,于是开始 merge :
此时远程仓库的状态:
当你的小伙伴将其代码merge 到master 后,这是你也开发完成了,也需要进行 merge 到
master 操作,于是你:
# 切换至 master分支, pull 一下,保证本地的master是最新内容。
# 合并前这么做是一个好习惯
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 分支
# 这么做是因为如果有冲突,可以在feature-1分支上进行处理,而不是在在master上解决冲突。
# 这么做是一个好习惯
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分支已经merge进来了新内容,为了保证远程分支最新,所以最好push一下。
# 2、要 push 的另一个原因是因为在实际的开发中,master的merge操作一般不是由我们自己在本地进
# 其他人员或某些平台merge时,操作的肯定是远程分支,所以就要保证远程分支的最新。
# 3、如果 merge 出现冲突,不要忘记需要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 分支推送至远端
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
此时远程仓库的状态:
此时, feature-1 和feature-2 分支对于我们来说就没用了, 那么我们可以直接在远程仓库中
将dev分支删除掉:
1.2远程分支删除后,本地 git branch -a 依然能看到的解决办法
当前我们已经删除了远程的几个分支,使用 git branch -a 命令可以查看所有本地分支和远程分
支,但发现很多在远程仓库已经删除的分支在本地依然可以看到。例如:
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地址,远程分支,还有本地分支与之相
对应关系等信息。
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)
此时我们可以看到那些远程仓库已经不存在的分支,根据提示,使用 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
这样就删除了那些远程仓库不存在的分支。