准备两台虚拟机:服务端、客户端
[root@z1 ~]# yum install -y epel-release git
创建用户,并设定shell:
[root@z1 ~]# useradd -s /usr/bin/git-shell git
#目的是不让git用户远程登录
切换至git家目录:
[root@z1 ~]# cd /home/git/
创建authorized_keys文件,并更改属主、数组权限,用来存放客户端机器上的公钥:
[root@z1 git]# mkdir .ssh
[root@z1 git]# vim .ssh/authorized_keys
#将客户端的公钥放入该文件
客户端生成密钥对的方法:
[root@z2 ~]# ssh-keygen
公钥位置
[root@z2 ~]# cat /root/.ssh/id_rsa.pub
更改权限:
[root@z1 git]# chown -R git .ssh
[root@z1 git]# chmod 700 .ssh
在客户端测试能否登录服务器:
[root@z2 ~]# ssh git@192.168.8.136
Are you sure you want to continue connecting (yes/no)? yes
出现该提示,说明可以登录,因为git用户的shell禁止该用户登录虚拟机,所以在此输入密码后直接退出。
[root@z1 git]# mkdir -p /data/gitroot
[root@z1 git]# cd /data/gitroot/
初始化:
[root@z1 gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
#创建一个名为sample.git的非隐藏的裸仓库(无法操作的库)
[root@z1 gitroot]# ls
sample.git
设置权限:
[root@z1 gitroot]# chown -R git.git sample.git
看第一节内容:
[root@z2 ~]# yum install -y epel-release git
客户端git搭建完成后执行如下操作:
克隆sample.git 库到本地:
[root@z2 gitroot]# git clone git@192.168.8.136:/data/gitroot/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一个空版本库。
[root@z2 gitroot]# ls
sample
[root@z2 gitroot]# cd sample/
[root@z2 sample]# ls -a
. .. .git
[root@z2 sample]# cd .git/
[root@z2 .git]# ls
branches config description HEAD hooks info objects refs
[root@z2 .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@192.168.8.136:/data/gitroot/sample.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[root@z2 .git]# cd /home/gitroot/sample
[root@z2 sample]# vim 1.txt
[root@z2 sample]# git add 1.txt
[root@z2 sample]# git commit -m "test file"
[master(根提交) 3b92484] test file
1 file changed, 3 insertions(+)
create mode 100644 1.txt
推送:
[root@z2 sample]# git push -u origin master
分支 master 设置为跟踪来自 origin 的远程分支 master。
Everything up-to-date
搭建完成!
(adsbygoogle = window.adsbygoogle || []).push({});