前言
一、多人协作开发
1.1 多人协作一
目前,我们所完成的工作如下:
- 基本完成Git的所有本地库的相关操作,git基本操作,分支理解,版本回退,冲突解决等
- 申请码云账号,将远端信息clone到本地,以及推送和拉取。
是时候干最重要的一件事情了,实现多人协作开发!!!
为了做这件事情,我们需要先做一些准备工作。
我们之前已经将项目clone到了指定目录,如:
代码示例:
hyb@139-159-150-152:~/git_teaching$ pwd/home/hyb/git_teaching我们在windows环境下,再clone同一个项目仓库,来模拟和你一起协作开发的另一名小伙伴:![]()
Clone 成功
注意,课件中是模拟了两个用户,实际开发中,每个用户都有自己的gitee/github账号,
如果要多人进行协同开发,必须要将用户添加进开发者,用户才有权限进行代码提交:
点击邀请用户:
到此,相当于有了两个用户,分别在linux和windows上针对于同项目进行协作开发,我们的准备工作到此结束。
目前,我们的仓库中只有一个master主分支,但在实际的项目开发中,在任何情况下其实都是不允许直接在master分支上修改代码的,这是为了保证主分支的稳定。
所以在开发新功能时,常常会新建其他分支,供开发时进行迭代使用。
那么接下来,就让我们在gitee 上新建dev远程分支供我们使用:
创建成功:
创建成功的远程分支是可以通过Git拉取到本地来,以实现完成本地开发工作。
接下来让我们和另一名开发的小伙伴都将远程仓库进行一次拉取操作,并观察结果:.
对于我们要操作的是:
代码示例:
hyb@139-159-150-152:~/git_teaching$ git pullFrom gitee.com:hyb91/git_teaching* [new branch] dev -> origin/devAlready up to date.# 注:之前讲的 git branch 其实只能查看本地分支,要查看远程分支需要加上-r选项。# 但前提是要pull⼀下拉取最新的远端仓库,才能看到最新的内容。hyb@139-159-150-152:~/git_teaching$ git branch -rorigin/HEAD -> origin/masterorigin/devorigin/masterhyb@139-159-150-152:~/git_teaching$ git checkout -b dev origin/devBranch 'dev' set up to track remote branch 'dev' from 'origin'.Switched to a new branch 'dev'代码说明:拉取后便可以看到远程的dev分支,接着切换到dev分支供我们进行本地开发。要说明的是,我们切换到的是本地的dev分支,根据示例中的操作,会将本地分支和远程分支的进行关系链接。对于小伙伴要操作的是:
现在,你和你的小伙伴就可以在dev上完成开发。
首先,让我们在dev分支上进行一次开发,并push到远程。
代码示例:
hyb@139-159-150-152:~/git_teaching$ vim file.txthyb@139-159-150-152:~/git_teaching$ cat file.txthello gitcomplete the first function!hyb@139-159-150-152:~/git_teaching$ git add file.txthyb@139-159-150-152:~/git_teaching$ git commit -m "first function"[dev 305f78a] first function1 file changed, 1 deletion(-)hyb@139-159-150-152:~/git_teaching$ git push origin dev # 将dev分⽀推送到远端Enumerating objects: 5, done.Counting objects: 100% (5/5), done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 287 bytes | 287.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.gitcc3be59..305f78a dev -> dev让我们来看看码云上目前仓库的状态:![]()
至此,我们已经将代码成功推送至码云,接下来假如你的小伙伴要和你协同开发,碰巧也要对file.txt文件作修改,并试图推送,例如:
图示说明:
这时推送失败,因为你的小伙伴的最新提交和你推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull/把最新的提交从 origin/dev抓下来,然后,在本地进行合并,并解决冲突,再推送。
代码操作如下:
解决冲突,重新推送。
代码示例如下:
此时,我们看到远端的码云已经能看到我们的新提交了!
图示示例:
由此,两名开发者已经开始可以进行协同开发了,不断的git pull/add/commit/push ,遇到了冲突,就使用我们之前讲的冲突处理解决掉冲突。
对于你来说,要想看到小伙伴的代码,只需要pull一下即可,
代码示例:
hyb@139-159-150-152:~/git_teaching$ cat file.txthello gitcomplete the first function!hyb@139-159-150-152:~/git_teaching$ git pullUpdating 305f78a..72c5345Fast-forwardfile.txt | 1 +1 file changed, 1 insertion(+)hyb@139-159-150-152:~/git_teaching$ cat file.txthello gitcomplete the first function!complete the second function!
最后不要忘记,虽然我们是在分支上进行多人协作开发,但最终的目的是要将开发后的代码合并到master上去,让我们的项目运行最新的代码。接下来我们就需要做这件事情了∶
代码示例:
# 切换至master分支, pull ⼀下,保证本地的master是最新内容。# 合并前这么做是⼀个好习惯hyb@139-159-150-152:~/git_teaching$ git checkout masterSwitched to branch 'master'Your branch is up to date with 'origin/master'.hyb@139-159-150-152:~/git_teaching$ git pullAlready up to date.# 切换至 dev 分支 合并 master 分支# 这么做是因为如果有冲突,可以在dev分支上进行处理,而不是在在master上解决冲突。# 这么做是⼀个好习惯hyb@139-159-150-152:~/git_teaching$ git checkout devSwitched to branch 'dev'Your branch is up to date with 'origin/dev'.hyb@139-159-150-152:~/git_teaching$ git merge masterAlready up to date.# 切换至 master 分支,合并 dev 分支hyb@139-159-150-152:~/git_teaching$ git checkout masterSwitched to branch 'master'Your branch is up to date with 'origin/master'.hyb@139-159-150-152:~/git_teaching$ git merge devUpdating 7388a31..72c5345Fast-forwardfile.txt | 2 ++1 file changed, 2 insertions(+)hyb@139-159-150-152:~/git_teaching$ cat file.txthello gitcomplete the first function!complete the second function!# 将 master 分支推送至远端hyb@139-159-150-152:~/git_teaching$ git statusOn branch masterYour branch is ahead of 'origin/master' by 4 commits.(use "git push" to publish your local commits)nothing to commit, working tree cleanhyb@139-159-150-152:~/git_teaching$ git push origin masterTotal 0 (delta 0), reused 0 (delta 0)remote: Powered by GITEE.COM [GNK-6.4]To gitee.com:hyb91/git_teaching.git7388a31..72c5345 master -> masterhyb@139-159-150-152:~/git_teaching$ git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean此时,查看远端仓库,master已经是最新代码了:
此时,dev分支对于我们来说就没用了,那么dev分支就可以被删除掉。我们可以直接在远程仓库中将dev分支删除掉:![]()
总结一下,在同一分支下进行多人协作的工作模式通常是这样:
首先,可以试图用git push origin branch-name推送自己的修改;
如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
如果合并有冲突,则解决冲突,并在本地提交;
没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
功能开发完毕,将分支merge进 master,最后删除分支。
1.2 多人协作二
一般情况下,如果有多需求需要多人同时进行开发,是不会在一个分支上进行多人开发,而是一个需求或一个功能点就要创建一个feature分支。
现在同时有两个需求需要你和你的小伙伴进行开发,那么你们俩便可以各自创建一个分支来完成自己的工作。
在上个部分我们已经了解了可以从码云上直接创建远程分支,其实在本地创建的分支也可以通过推送的方式发送到远端。在这个部分我们就来用一下这种方式。
对于你来说,可以进行以下操作:
代码示例:
# 新增本地分支 feature-1 并切换hyb@139-159-150-152:~/git_teaching$ git branchdev* masterhyb@139-159-150-152:~/git_teaching$ git checkout -b feature-1Switched to a new branch 'feature-1'# 新增需求内容-创建function1文件hyb@139-159-150-152:~/git_teaching$ vim function1hyb@139-159-150-152:~/git_teaching$ cat function1Done!# 将 feature-1 分支推送到远端hyb@139-159-150-152:~/git_teaching$ git add function1hyb@139-159-150-152:~/git_teaching$ git commit -m"add function1"[feature-1 12ed0db] add function11 file changed, 1 insertion(+)create mode 100644 function1hyb@139-159-150-152:~/git_teaching$ git push origin feature-1Enumerating 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...hyb9To 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_teaching305f78a..72c5345 dev -> origin/dev* [new branch] feature-2 -> origin/feature-2...# 可以看到远程已经有了feature-2hyb@139-159-150-152:~/git_teaching$ git branch -adev* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/feature-2remotes/origin/master# 切换到feature-2分⽀上,可以和远程的feature-2分⽀关联起来,# 否则将来只使⽤ git push 推送内容会失败hyb@139-159-150-152:~/git_teaching$ git checkout -b feature-2 origin/feature-2Branch '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$ lsa.so b.ini file.txt function2 README.en.md README.md切换成功后,便可以看见 feature-2 分支中的 function2 文件了,接着就可以帮小伙伴进行开发:代码示例:# 继续开发hyb@139-159-150-152:~/git_teaching$ vim function2hyb@139-159-150-152:~/git_teaching$ cat function2Done!Help done!# 推送内容hyb@139-159-150-152:~/git_teaching$ git add function2hyb@139-159-150-152:~/git_teaching$ git commit -m"modify function2"[feature-2 1079ae7] modify function21 file changed, 2 insertions(+), 1 deletion(-)hyb@139-159-150-152:~/git_teaching$ git push origin feature-2Enumerating 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.gite1233f1..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 masterSwitched to branch 'master'Your branch is up to date with 'origin/master'.hyb@139-159-150-152:~/git_teaching$ git pullFrom gitee.com:hyb91/git_teaching72c5345..29006bd master -> origin/masterUpdating 72c5345..29006bdFast-forwardfunction2 | 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-1Switched to branch 'feature-1'Your branch is up to date with 'origin/feature-1'.hyb@139-159-150-152:~/git_teaching$ git merge masterMerge made by the 'recursive' strategy.function2 | 2 ++1 file changed, 2 insertions(+)create mode 100644 function2hyb@139-159-150-152:~/git_teaching$ lsa.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 statusOn branch feature-1Your branch is ahead of 'origin/feature-1' by 4 commits.(use "git push" to publish your local commits)nothing to commit, working tree cleanhyb@139-159-150-152:~/git_teaching$ git push origin feature-1Enumerating 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.gitea75a35..4b4c3d4 feature-1 -> feature-1# 切换⾄ master 分⽀,合并 feature-1 分⽀hyb@139-159-150-152:~/git_teaching$ git checkout masterSwitched to branch 'master'Your branch is up to date with 'origin/master'.hyb@139-159-150-152:~/git_teaching$ git merge feature-1Updating 29006bd..4b4c3d4Fast-forwardfunction1 | 1 +1 file changed, 1 insertion(+)create mode 100644 function1hyb@139-159-150-152:~/git_teaching$ lsa.so b.ini file.txt function1 function2 README.en.md README.md# 将 master 分⽀推送⾄远端hyb@139-159-150-152:~/git_teaching$ git statusOn branch masterYour branch is ahead of 'origin/master' by 3 commits.(use "git push" to publish your local commits)nothing to commit, working tree cleanhyb@139-159-150-152:~/git_teaching$ git push origin masterTotal 0 (delta 0), reused 0 (delta 0)remote: Powered by GITEE.COM [GNK-6.4]To gitee.com:hyb91/git_teaching.git29006bd..4b4c3d4 master -> masterhyb@139-159-150-152:~/git_teaching$ git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean此时远程仓库的状态:![]()
此时,feature-1和feature-2分支对于我们来说就没用了,那么我们可以直接在远程仓库中将dev分支删除掉:
这就是多人协作的工作模式,一旦熟悉了,就非常简单。
1.3 远程分支删除后,本地 git branch -a 依然能看到的解决办法
当前我们已经删除了远程的几个分支,使用git branch -a命令可以查看所有本地分支和远程分支,但发现很多在远程仓库已经删除的分支在本地依然可以看到。
代码举例:
hyb@139-159-150-152:~/git_teaching$ git pullAlready up to date.hyb@139-159-150-152:~/git_teaching$ git branch -adevfeature-1feature-2* masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/feature-2remotes/origin/master
使用命令git remote show origin,可以查看remote地址,远程分支,还有本地分支与之相对应关系等信息。
代码示例:
hyb@139-159-150-152:~/git_teaching$ git remote show origin* remote originFetch URL: git@gitee.com:hyb91/git_teaching.gitPush URL: git@gitee.com:hyb91/git_teaching.gitHEAD branch: masterRemote branches:master trackedrefs/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 devfeature-1 merges with remote feature-1feature-2 merges with remote feature-2master merges with remote masterLocal ref configured for 'git push':master pushes to master (up to date)
此时我们可以看到那些远程仓库已经不存在的分支,
根据提示,使用git remote pruneorigin命令:
hyb@139-159-150-152:~/git_teaching$ git remote prune originPruning originURL: git@gitee.com:hyb91/git_teaching.git* [pruned] origin/dev* [pruned] origin/feature-1* [pruned] origin/feature-2hyb@139-159-150-152:~/git_teaching$ git branch -adevfeature-1feature-2* masterremotes/origin/HEAD -> origin/masterremotes/origin/master这样就删除了那些远程仓库不存在的分支。remotes/origin/devremotes/origin/feature-1remotes/origin/feature-2对于本地仓库的删除,之前的课程已经学过了,大家可以自行从操作。