Git -- 创建Git服务器

如果与其它人合作进行开发工作(例如在公司中),那就需要一个共享的Git仓库,开发工程师们都可以访问,从这个共享仓库中推送和拉取数据,这个仓库就称之为“Git服务器”。

创建Git服务器

创建Git服务器是比较简单的,测试环境使用的是两台CentOS7,IP分别为192.168.107.128(测试服务器)、192.168.107.129(测试客户端)。
首先在测试服务器上进行操作:
1、添加一个新用户git
这个新添加的用户,是用来供测试客户端与服务器端通信使用的(通信使用SSH协议),即客户端进行pull或push时都是以git用户的权限来进行操作。

[root@localhost ~]# useradd git
[root@localhost ~]# passwd git

用户创建完成后记得使用passwd来设置密码。
2、创建Git Repository
新创建的用户会在/home目录下生成相应的目录,进入到/home/git/目录下进行操作:

[root@localhost ~]# cd /home/git/
[root@localhost git]# mkdir repositories
[root@localhost git]# cd repositories/
[root@localhost repositories]# git --bare init
初始化空的 Git 版本库于 /home/git/repositories/
[root@localhost repositories]# cd ..
[root@localhost git]# chown -R git:git repositories/

git –bare init:创建一个空的Git仓库,这个仓库能够提供服务但不会在此服务器上进行操作;
chown -R git:git repositories/:设置Git仓库的所有者为用户git。
3、设置authorized_keys
authorized_keys是个配置文件,用来存放测试客户端的ssh-key。这个文件应存放于测试服务器的/home/git/目录下,自行创建”.ssh”目录:

[root@localhost git]# mkdir .ssh
[root@localhost git]# cd .ssh
[root@localhost .ssh]# vi authorized_keys

在测试客户端运行”ssh-keygen”然后一路Enter,默认会存放于“/root/.ssh/”目录中,拷贝其中的“id_rsa.pub”文件中的key信息到authorized_keys文件中即可。
好了,到这里服务器端的工作就算是完成了,已经有了一个基本的Git服务器可以使用了。

客户端操作

Git服务器创建完成后,切换到测试客户端来进行一下测试:

[root@localhost ~]# mkdir work
[root@localhost ~]# cd work/
[root@localhost work]# git clone git@192.168.107.128:/home/git/repositories
[root@localhost work]# ls
repositories
[root@localhost work]# cd repositories/
[root@localhost repositories]# ls
[root@localhost repositories]# 

如上,在测试客户端创建了一个work目录,在该目录下进行了clone操作,操作成功,获取了一个空的版本库(Git仓库中还没有文件,当然是空的)。

[root@localhost repositories]# echo "hello,world" > hi
[root@localhost repositories]# ls
hi
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add file hi"
[root@localhost repositories]# git push origin master

创建了一个名为“hi”的文件,输入“hello,world”,然后在本地进行添加和提交,最后将它推送到远端的Git服务器仓库master分支。
这时,如果有一个新的客户端clone了这个Git仓库,就不会clone到一个空的仓库了,仓库中会出现一个“hi”的文件,内容是“hello,world”。

解决版本冲突

版本冲突是仓库中经常遇到的问题,例如两个客户端从同一个服务器clone,客户端A修改了“hi”文件,追加了一行”AAAAAA“,提交并推送到了Git服务器;客户端B也修改了”hi“文件,追加了一行”BBBBBB“,也提交并向Git服务器推送,但此时客户端B的推送是不成功的,Git服务器会提醒客户端B”Git服务器中的版本与客户端B上的版本有冲突“。
客户端A操作:

[root@localhost repositories]# echo "AAAAAA" >> hi
[root@localhost repositories]# cat hi 
hello,world
AAAAAA
[root@localhost repositories]# 
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add 'AAAAAA'"
[master 84beedf] add 'AAAAAA'
 1 file changed, 1 insertion(+), 2 deletions(-)
[root@localhost repositories]# 
[root@localhost repositories]# git push origin master

客户端B操作:

[root@localhost repositories]# echo "BBBBBB" >> hi
[root@localhost repositories]# cat hi
hello,world
BBBBBB
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "add 'BBBBBB'"
[master 1070263] add 'BBBBBB'
 1 file changed, 1 insertion(+), 2 deletions(-)
[root@localhost repositories]# git push origin master

此时需要在客户端B上拉取Git服务器中的版本,解决冲突后再提交并推送到Git服务器:

[root@localhost repositories]# git pull
[root@localhost repositories]# cat hi
hello,world
<<<<<<< HEAD
BBBBBB
=======
AAAAAA
>>>>>>> 84beedf9597880a33219182b34146831539915e1
[root@localhost repositories]# vi hi
[root@localhost repositories]# cat hi
hello,world
BBBBBB
AAAAAA
[root@localhost repositories]# git add hi
[root@localhost repositories]# git commit -m "resolve conflict"
[master f632248] resolve conflict
[root@localhost repositories]# git push origin master

至此,冲突解决并将最新的修改推送到了Git服务器上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值