博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
08 分支管理 —— 多人协作
阅读量:7239 次
发布时间:2019-06-29

本文共 6799 字,大约阅读时间需要 22 分钟。

hot3.png

08 分支管理 —— 多人协作

多人协作

本节内容:

查看远程库信息,使用git remote -v;本地新建的分支如果不推送到远程,对其他人就是不可见的;从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin

要查看远程库的信息,用git remote

lwenhaodeMacBook-Pro:TestGit lwenhao$ git remoteoriginlwenhaodeMacBook-Pro:TestGit lwenhao$

或者用git remote -v显示更详细的信息:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git remote -vorigin	https://github.com/liuwenhaoCN/TestGit.git (fetch)origin	https://github.com/liuwenhaoCN/TestGit.git (push)lwenhaodeMacBook-Pro:TestGit lwenhao$

上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

推送分支

推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样Git就会把该分支推送到远程库对应的远程分支上:

git push origin master

如果要推送其他分支,比如dev,就改成:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin devTotal 0 (delta 0), reused 0 (delta 0)remote: This repository moved. Please use the new location:remote:   https://github.com/lwenhaoCN/TestGit.gitremote: remote: Create a pull request for 'dev' on GitHub by visiting:remote:      https://github.com/lwenhaoCN/TestGit/pull/new/devremote: To https://github.com/liuwenhaoCN/TestGit.git * [new branch]      dev -> devlwenhaodeMacBook-Pro:TestGit lwenhao$

但是,并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?

  • master分支是主分支,因此要时刻与远程同步;
  • dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
  • bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;
  • feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

总之,就是在Git中,分支完全可以在本地自己藏着玩,是否推送,视你的心情而定!

抓取分支

多人协作时,大家都会往masterdev分支上推送各自的修改。

现在,模拟一个你的小伙伴,可以在另一台电脑(注意要把SSH Key添加到GitHub)或者同一台电脑的另一个目录下克隆:新建Test文件夹,把TestGit克隆到该文件夹(这个文件夹模拟你的小伙伴)。

lwenhaodeMacBook-Pro:Test lwenhao$ git clone 'https://github.com/lwenhaoCN/TestGit.git'Cloning into 'TestGit'...remote: Enumerating objects: 23, done.remote: Counting objects: 100% (23/23), done.remote: Compressing objects: 100% (10/10), done.remote: Total 23 (delta 3), reused 20 (delta 3), pack-reused 0Unpacking objects: 100% (23/23), done.lwenhaodeMacBook-Pro:Test lwenhao$

当你的小伙伴从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支。使用git branch命令查看:

lwenhaodeMacBook-Pro:Test lwenhao$ cd TestGit/lwenhaodeMacBook-Pro:TestGit lwenhao$ git branch* masterlwenhaodeMacBook-Pro:TestGit lwenhao$

现在,你的小伙伴要在dev分支上开发,就必须创建远程origindev分支到本地,于是他用这个命令创建本地dev分支:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout -b dev origin/devBranch 'dev' set up to track remote branch 'dev' from 'origin'.Switched to a new branch 'dev'lwenhaodeMacBook-Pro:TestGit lwenhao$

并且你的小伙伴修改README.md文件内容:

lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit创建一个"dev"分支,我来操作。学习分支管理策略。dev、dev、devlwenhaodeMacBook-Pro:TestGit lwenhao$

他就可以在dev上继续修改,然后时不时地把dev分支push到远程:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "add dev"[dev 0f59233] add dev 1 file changed, 2 insertions(+)lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin devCounting objects: 3, done.Delta compression using up to 12 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 267 bytes | 267.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.To https://github.com/lwenhaoCN/TestGit.git   8faa495..0f59233  dev -> devlwenhaodeMacBook-Pro:TestGit lwenhao$

你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对同样的文件作了修改,并试图推送:

现在返回到你原来的TestGit中,并且修改README.md内容:

lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit创建一个"dev"分支,我来操作。学习分支管理策略。我自己写的。lwenhaodeMacBook-Pro:TestGit lwenhao$

你自己提交dev分支下的README.md

lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "add my"[dev 29bc6cb] add my 1 file changed, 2 insertions(+)lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin devTo https://github.com/liuwenhaoCN/TestGit.git ! [rejected]        dev -> dev (fetch first)error: failed to push some refs to 'https://github.com/liuwenhaoCN/TestGit.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.lwenhaodeMacBook-Pro:TestGit lwenhao$

推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后在本地合并,解决冲突,再推送:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git pullremote: Enumerating objects: 5, done.remote: Counting objects: 100% (5/5), done.remote: Compressing objects: 100% (1/1), done.remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0Unpacking objects: 100% (3/3), done.From https://github.com/liuwenhaoCN/TestGit   8faa495..0f59233  dev        -> origin/devThere is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details.    git pull 
If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/
devlwenhaodeMacBook-Pro:TestGit lwenhao$

git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置devorigin/dev的链接:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git branch --set-upstream-to=origin/dev devBranch 'dev' set up to track remote branch 'dev' from 'origin'.lwenhaodeMacBook-Pro:TestGit lwenhao$

在使用git pull

lwenhaodeMacBook-Pro:TestGit lwenhao$ git pullAuto-merging README.mdCONFLICT (content): Merge conflict in README.mdAutomatic merge failed; fix conflicts and then commit the result.lwenhaodeMacBook-Pro:TestGit lwenhao$

这回git pull成功,但是合并有冲突,需要手动解决,解决的方法和的完全一样。解决后,提交,再push:

lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "fix README.md conflict"[dev cd4d07d] fix README.md conflictlwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin devCounting objects: 6, done.Delta compression using up to 12 threads.Compressing objects: 100% (4/4), done.Writing objects: 100% (6/6), 524 bytes | 524.00 KiB/s, done.Total 6 (delta 2), reused 0 (delta 0)remote: Resolving deltas: 100% (2/2), completed with 1 local object.remote: This repository moved. Please use the new location:remote:   https://github.com/lwenhaoCN/TestGit.gitTo https://github.com/liuwenhaoCN/TestGit.git   0f59233..cd4d07d  dev -> devlwenhaodeMacBook-Pro:TestGit lwenhao$

因此,多人协作的工作模式通常是这样:

  1. 首先,可以试图用git push origin <branch-name>推送自己的修改;
  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
  3. 如果合并有冲突,则解决冲突,并在本地提交;
  4. 没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!

如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>

转载于:https://my.oschina.net/lwenhao/blog/3013334

你可能感兴趣的文章
Spring和springMVC父子容器的关系
查看>>
计算机图形学常用算法
查看>>
systemd自启动java程序
查看>>
Spring Boot 项目中使用JSP
查看>>
TensorFlow-谷歌深度学习库 体验一二三
查看>>
SSM架包
查看>>
corte-m3中的pc值的问题
查看>>
[TCO2013]TrickyInequality
查看>>
序列化之Parcelable
查看>>
运算符优先级
查看>>
多任务-进程之PID
查看>>
C# 隐藏最大化、最小化和关闭三个按钮
查看>>
devcloud
查看>>
Codeforces 1139D(期望dp)
查看>>
解决AX2012通过AXC配置文件打开客户端时出现WCF 错误
查看>>
课题申请经验简单总结
查看>>
Javascript Array Distinct (array.reduce实现)
查看>>
[SOJ] Ordering Tasks
查看>>
Navicat 导出sql问题
查看>>
3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
查看>>