【Git】8. 多⼈协作⼆

多⼈协作⼆

⼀般情况下,如果有多需求需要多⼈同时进⾏开发,是不会在⼀个分⽀上进⾏多⼈开发,⽽是⼀个需求或⼀个功能点就要创建⼀个 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分⽀删除掉:
在这里插入图片描述
这就是多⼈协作的⼯作模式,⼀旦熟悉了,就⾮常简单。

远程分⽀删除后,本地 git branch -a 依然能看到的解决办法

查看本地+远端全部分支 – 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

使⽤命令 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

此时我们可以看到那些远程仓库已经不存在的分⽀,根据提⽰,使⽤ 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

这样就删除了那些远程仓库不存在的分⽀。对于本地仓库的删除,之前的博客中已经介绍了,⼤家可以⾃⾏操作。

  • 11
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值