本文共 3425 字,大约阅读时间需要 11 分钟。
搭建git服务器
服务端192.168.221.20,安装git,创建用户,创建文件authorized_keys,权限,属主属组
[root@apenglinux-002 yum.repos.d]# yum install git -y[root@apenglinux-002 yum.repos.d]# useradd -s /usr/bin/git-shell git[root@apenglinux-002 yum.repos.d]# cd /home/git/[root@apenglinux-002 git]# mkdir .ssh[root@apenglinux-002 git]# touch .ssh/authorized_keys[root@apenglinux-002 git]# chmod 600 .ssh/authorized_keys[root@apenglinux-002 git]# chown -R git:git .ssh/
客户端192.168.221.10,将公钥放到服务端.ssh/authorized_keys
[root@localhost apeng]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCZ4he1cZhRT7LHd5QXtdMqA2g0ub+tp9uau0gG6rN5cRcHZhJv4vyFhuyhFSR4dHNPzMg+j5KSfWtkhikW52NXZ/+sgYC2/aS7C+vTBZZx82fL2YiF6eDOB0VZR6yTZggWQjljne8sle1W6wQiHkshbRFeV+pg4sYTXI2LHlzFcsFuyOhF4L6Cykg/v9s8ZvotRGRMLWBHAzCj+iI0pzwCJrHMzN6yiV6JH9ZyN3IrQs2cEoy+dbV/UxztPIc5f6tns+5jGeQtkUL102c+NFQ19qmU1kU+lbZ91+/42pbndJrrdyCdpeAmMDQSqe+7/M+gOwDCKyjNZigeKfLv0tXH root@localhost.localdomain
服务端保存客户端的公钥
vim .ssh/authorized_keys //放入客户端的公钥
客户端连接服务端
[root@localhost apeng]# ssh git@192.168.221.20The authenticity of host '192.168.221.20 (192.168.221.20)' can't be established.ECDSA key fingerprint is SHA256:UiIDDUfExrEZLxrI8+z6PWjWsNCO2GTDDfTKpEhQaWY.ECDSA key fingerprint is MD5:4e:79:bd:c6:bb:8d:b7:ee:1a:a4:cb:25:03:22:10:5f.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.221.20' (ECDSA) to the list of known hosts.fatal: Interactive git shell is not enabled.hint: ~/git-shell-commands should exist and have read and execute access.Connection to 192.168.221.20 closed.
服务端建立裸仓库,修改仓库的属主,属组
[root@apenglinux-002 git]# mkdir -p /data/gitroot[root@apenglinux-002 git]# cd /data/gitroot[root@apenglinux-002 gitroot]# git init --bare sample.git初始化空的 Git 版本库于 /data/gitroot/sample.git/[root@apenglinux-002 gitroot]# lssample.git[root@apenglinux-002 gitroot]# chown -R git:git sample.git/
客户端克隆远程的仓库,建立一些文件推送到服务端
[root@localhost ~]# mkdir /copy[root@localhost ~]# cd /copy[root@localhost copy]# git clone git@192.168.221.20:/data/gitroot/sample.git[root@localhost copy]# cd sample/[root@localhost sample]# echo "client" > client.txt[root@localhost sample]# git add client.txt[root@localhost sample]# git commit -m "add client.txt"[master(根提交) 61a074d] add client.txt 1 file changed, 1 insertion(+) create mode 100644 client.txt [root@localhost sample]# git push
[root@localhost sample]# git push origin master Counting objects: 3, done.Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@192.168.221.20:/data/gitroot/sample.git * [new branch] master -> master
在客户端上再次建立一些文件,看看情况
[root@localhost sample]# echo "client1" > client1.txt[root@localhost sample]# git add client1.txt[root@localhost sample]# git commit -m "add client1.txt"[root@localhost sample]# git push
在客户端上将服务端上的新的变更克隆到另外一个目录下
[root@localhost sample]# mkdir /update[root@localhost sample]# cd /update[root@localhost update]# git clone git@192.168.221.20:/data/gitroot/sample.git[root@localhost update]# ls sample/client1.txt client.txt
修改/copy/sample/client1.txt文件,推送到服务端,到/update/sample获取更新的数据
[root@localhost sample]# git add client1.txt[root@localhost sample]# git commit -m "modify client1.txt"[master a89467d] modify client1.txt 1 file changed, 1 insertion(+)[root@localhost sample]# git push[root@localhost sample]# cd /update/sample/[root@localhost sample]# git pull
转载于:https://blog.51cto.com/13480443/2090659