Git的多人协作和分支处理测试

首先配置ssh密钥

克隆项目

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

创建分支

[root@ ~/Git_test_多人协作和冲突合并/branch_test]$ ls
README.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]$ ls
README.md
[root@ ~/branch_test]$ git checkout yun
切换到分支 'yun'
[root@ ~/branch_test]$ git branch
  master
* yun
[root@ ~/branch_test]$ ls
README.md
[root@ ~/branch_test]$ touch yun1.c
[root@ ~/branch_test]$ ls
README.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]$ ls
README.md  yun1.c
[root@ ~/branch_test]$ cat yun1.c 
hello world
[root@ ~/branch_test]$ git push origin master
Total 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 Bytes
hello world
  • 我们使用mac进行提交
khz:branch_test kouhz$ ls
README.md	yun1.c
khz:branch_test kouhz$ vim yun1.c 
khz:branch_test kouhz$ cat yun1.c 
HELLO WORLD

khz: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 push
To 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 do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (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 查看文件的不同
<<<<<<< HEAD
HELLO WORLD
=======
hello world
>>>>>>> 8d4d57c701bfbda71cc442caf2017c01437a80d1

其他分支

yun 分支添加文字并提交

[root@ ~/branch_test]$ git branch
  master
* yun
[root@ ~/branch_test]$ ls
README.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 push
Counting 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=yun
remote: 
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 
<<<<<<< HEAD
hello world
HELLO WORLD
=======
yun hello
>>>>>>> 4d7c92c5686128aa3c99c25f3dbfdbbd88f8cf5e
khz:branch_test kouhz$ vim yun1.c 
khz:branch_test kouhz$ cat yun1.c 
yun hello
hello world
HELLO WORLD

khz:branch_test kouhz$ git add yun1.c 
khz:branch_test kouhz$ git commit -m "mac"
[yun ae006aa] mac
khz:branch_test kouhz$ git push origin yun
Counting 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=yun
remote: 
To git.github.com:kouhaozhe/branch_test.git
   4d7c92c..ae006aa  yun -> yun

切换到另一台主机

  • 进行git pull操作,结果失败了!原因是没有指定本地dev分支与远程origin/dev分支的链接
[root@ ~/branch_test]$ git pull
remote: 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/yun
There 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 <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> 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..ae006aa
Fast-forward
 yun1.c | 2 ++
 1 file changed, 2 insertions(+)
[root@ ~/Git_test_多人协作和冲突合并/bra
nch_test]$ cat yun1.c 
yun hello
hello world
HELLO WORLD

如何一次性永久保存密码

git config --global credential.helper store

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值