博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git的多人协作和分支处理测试
阅读量:2500 次
发布时间:2019-05-11

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

首先配置ssh密钥

克隆项目

  • 配置两台主机(一台本地mac,一台云服务器)通过这样的方式模拟多人开发。

创建分支

[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ lsREADME.md[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ git branch* master[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ git branch yun[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ git branch* master  yun[root@ ~/Git_test_多人协作和冲突合并/branch_test]$

转移到分支并在分支添加项目

[root@ ~/branch_test]$ lsREADME.md[root@ ~/branch_test]$ git checkout yun切换到分支 'yun'[root@ ~/branch_test]$ git branch  master* yun[root@ ~/branch_test]$ lsREADME.md[root@ ~/branch_test]$ touch yun1.c[root@ ~/branch_test]$ lsREADME.md  yun1.c[root@ ~/branch_test]$ git add yun1.c[root@ ~/branch_test]$ git commit -m "yun1.c"*** Please tell me who you are.Run  git config --global user.email "you@example.com"  git config --global user.name "Your Name"to set your account's default identity.Omit --global to set the identity only in this repository.fatal: empty ident name  not allowed

要提交自己的用户名和邮箱才可以继续

  • 提交完毕后
[root@ ~/branch_test]$ git commit -m "yun1.c"                   [yun eb267c4] yun1.c 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 yun1.c

冲突测试

master

  • 首先使用yun提交一段代码
[root@ ~/branch_test]$ git branch * master  origin  yun[root@ ~/branch_test]$ lsREADME.md  yun1.c[root@ ~/branch_test]$ cat yun1.c hello world[root@ ~/branch_test]$ git push origin masterTotal 0 (delta 0), reused 0 (delta 0)To git@git.github.com:kouhaozhe/branch_test.git   eb267c4..025a01e  master -> master
  • 这是master的yun1.c文件就有了一段代码
yun1.c 12 Byteshello world
  • 我们使用mac进行提交
khz:branch_test kouhz$ lsREADME.md	yun1.ckhz:branch_test kouhz$ vim yun1.c khz:branch_test kouhz$ cat yun1.c HELLO WORLDkhz:branch_test kouhz$ git add yun1.c khz:branch_test kouhz$ git commit -m "HELLO WORLD"[master 75da4f2] HELLO WORLD 1 file changed, 1 insertion(+)khz:branch_test kouhz$ git pushTo git.github.com:kouhaozhe/branch_test.git ! [rejected]        master -> master (fetch first)error: failed to push some refs to 'git@git.github.com:kouhaozhe/branch_test.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.khz:branch_test kouhz$

冲突时我们要先git pull再git push

  • git pull
  • git diff 查看文件的不同
<<<<<<< HEADHELLO WORLD=======hello world>>>>>>> 8d4d57c701bfbda71cc442caf2017c01437a80d1

其他分支

yun 分支添加文字并提交

[root@ ~/branch_test]$ git branch  master* yun[root@ ~/branch_test]$ lsREADME.md  yun1.c[root@ ~/branch_test]$ vim yun1.c [root@ ~/branch_test]$ cat yun1.c yun hello[root@ ~/branch_test]$ git add yun1.c[root@ ~/branch_test]$ git commit -m "yun yun1.c"[yun 4d7c92c] yun yun1.c 1 file changed, 1 insertion(+), 1 deletion(-)[root@ ~/branch_test]$ git pushCounting objects: 5, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)remote: remote: To create a merge request for yun, visit:remote:   http://git.github.com/kouhaozhe/branch_test/merge_requests/new?merge_request%5Bsource_branch%5D=yunremote: To git@git.github.com:kouhaozhe/branch_test.git   025a01e..4d7c92c  yun -> yun[root@ ~/Git_test_多人协作和冲突合并/branch_test]$
  • 这时网页已经有了我们提交的带着,我们切换到另一台机器
    另一台电脑除了自己创建的分支,看不见另一台电脑创建的分支,这时使用命令
khz:branch_test kouhz$ git branch  mac* master

同步之后没有yun这个分支,操作命令

切换另一台主机创建的分支

git checkout -b yun origin/yun

Tips:

error: you need to resolve your current index first

khz:branch_test kouhz$ git reset --merge

khz:branch_test kouhz$ git checkout -b yun origin/yun
Branch ‘yun’ set up to track remote branch ‘yun’ from ‘origin’.
Switched to a new branch ‘yun’

在进行添加代码

khz:branch_test kouhz$ cat yun1.c <<<<<<< HEADhello worldHELLO WORLD=======yun hello>>>>>>> 4d7c92c5686128aa3c99c25f3dbfdbbd88f8cf5ekhz:branch_test kouhz$ vim yun1.c khz:branch_test kouhz$ cat yun1.c yun hellohello worldHELLO WORLDkhz:branch_test kouhz$ git add yun1.c khz:branch_test kouhz$ git commit -m "mac"[yun ae006aa] mackhz:branch_test kouhz$ git push origin yunCounting objects: 6, done.Delta compression using up to 4 threads.Compressing objects: 100% (4/4), done.Writing objects: 100% (6/6), 577 bytes | 577.00 KiB/s, done.Total 6 (delta 0), reused 0 (delta 0)remote: remote: To create a merge request for yun, visit:remote:   http://git.github.com/kouhaozhe/branch_test/merge_requests/new?merge_request%5Bsource_branch%5D=yunremote: To git.github.com:kouhaozhe/branch_test.git   4d7c92c..ae006aa  yun -> yun

切换到另一台主机

  • 进行git pull操作,结果失败了!原因是没有指定本地dev分支与远程origin/dev分支的链接
[root@ ~/branch_test]$ git pullremote: Counting objects: 6, done.remote: Compressing objects: 100% (4/4), done.remote: Total 6 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (6/6), done.来自 git.github.com:kouhaozhe/branch_test   4d7c92c..ae006aa  yun        -> origin/yunThere 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/
yun
  • 我们按照提示进行操作
    git branch --set-upstream-to=origin/ yun
    git branch --set-upstream-to=origin/yun yun

这时操作就成功了

[root@ ~/branch_test]$ cat yun1.c yun hello[root@ ~/branch_test]$ git pull更新 4d7c92c..ae006aaFast-forward yun1.c | 2 ++ 1 file changed, 2 insertions(+)[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ cat yun1.c yun hellohello worldHELLO WORLD

如何一次性永久保存密码

git config --global credential.helper store

转载地址:http://saorb.baihongyu.com/

你可能感兴趣的文章
JAVA字符串格式化-String.format()的使用
查看>>
编译时类型 和运行时类型的 区别(1)
查看>>
php pcntl 多进程学习
查看>>
C++类指针类型的成员变量的浅复制与深复制
查看>>
看看清华的同学在四年的大学中干什么吧,非常值得学习
查看>>
Java和.NET(C#)的开发用到的技术对比总结
查看>>
关于strassen矩阵乘法的矩阵大小不是2^k的形式时,时间复杂度是否还是比朴素算法好的看法...
查看>>
vl_sift函数用法
查看>>
Ios17个常用代码整理
查看>>
适配ios7
查看>>
项目复审——Beta阶段
查看>>
Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)
查看>>
在Android App中集成Google登录
查看>>
openstack quantum搭建过程中一些有用的链接
查看>>
数据库:mysql 获取刚插入行id[转]
查看>>
Egret入门学习日记 --- 第二篇
查看>>
前端“黑话”polyfill
查看>>
wifi-mac
查看>>
linux 下运行 tomcat
查看>>
RocketMQ 使用及常见问题
查看>>