Linux搭建git服务端

1、安装
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git

2、接下来我们 创建一个git用户组和用户,用来运行git服务:
$ groupadd git
$ useradd git -g git
$ passwd ******

3、创建git仓库
$ cd /home/git
$ git init --bare gitrepo.git
$ chown -R git:git gitrepo.git

4、禁用shell登录(为安全考虑,git用户不允许登录shell)
#修改/etc/passwd
$ vim /etc/passwd
#找到git的用户设置 如:
git:x:502:503::/home/git:/bin/bash
改为
git:x:502:503::/home/git:/bin/git-shell
#这样用户用git账户ssh连接后只能使用git命令了

5、打开 RSA 认证
进入 /etc/ssh 目录,编辑 sshd_config,打开以下三个配置的注释:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
保存并重启 sshd 服务:
$ /etc/rc.d/init.d/sshd restart

6、客户端安装git并在(window的git bash)git操作提交
#进入一个空的工作目录
#设置用户信息:
$ git config --global user.name "username"
$ git config --global user.email "your@example.com"

7、#初始化git
$ git init

8、管理公钥
$ ssh-keygen -C 'your@email.com' -t rsa #为你生成rsa密钥,可以直接一路回车,执行默认操作
客户端生成密要方式同上。
生成密钥后,一般C:Users用户名.ssh会出现

.ssh
├── id_rsa
└── id_rsa.pub #公钥 服务端需要里边内容验证连接着身份
将id_rsa.pub 里边内容复制到authorized_keys

如找不到authorized_keys文件,则需在服务器端新建
$ cd /home/git
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys

在客户端上,打开 id_rsa.pub 复制里边内容,一行一个
$ vim /home/git/.ssh/authorized_keys
#粘贴客户端生成的公钥,保存退出

9、测试本地文件提交到远程仓库

$ vim readme.md
#编辑些内容保存退出
$ git add readme.md #添加到git缓存中
$ git commit -m 'write readme file' #提交修改
#添加远程git仓库
$ git remote add origin git@your_host_name:/home/git/gitrepo.git
$ git push origin master #这样就同步到服务器了
其他人要同步

#克隆和推送:
$ git clone git@your_host_name:/home/git/gitrepo.git
$ vim readme.md
$ git commit -am 'fix for the README file'
$ git push origin master
代码同步(HOOK)
上边git用于做了中心的版本控制
但是还想让服务器接到修改更新后自动同步代码到网站目录中,便于测试开发
如下操作是可以实现

#假定网站目录在/www/web下
cd /home/git/gitrepo.git/hooks
vim post-receive #创建一个钩子
#写入下面内容
GIT_WORK_TREE=/www/web git checkout -f
#保存退出
chown git:git post-receive
chmod +x post-receive
如此,下次提交修改,代码会自动同步到指定目录中

结合idea上传本地版本到远程仓库,参考https://blog.csdn.net/autfish/article/details/52513465

原文地址:https://www.cnblogs.com/wangfg/p/9501441.html