创建git远程仓库并实现push自动同步到站点目录

1、创建裸版本库:

[root@localhost]$ cd /opt/
[root@localhost]$ mkdir wwwroot.git
[root@localhost]$ git init --bare wwwroot.git

修改用户组

[root@localhost]$ chown -R git:git wwwroot.git

2、禁用shell登录:

出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:

git:x:1001:1001:,,,:/home/git:/bin/bash

改为:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

3、创建证书登录:

收集所有需要登录的用户的公钥,就是他们自己的id_rsa.pub文件,把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

4、克隆远程仓库

git clone git@server:/opt/wwwroot.git

5、设置钩子

cd /home/opt/wwwroot.git
cd hooks
//这里创建post-receive文件
vim post-receive
//在该文件里输入以下内容
#!/bin/bash
git --work-tree=/home/wwwroot checkout -f
//保存退出后,将该文件用户及用户组都设置成git
chown git:git post-receive
//由于该文件其实就是一个shell文件,我们还应该为其设置可执行权限
chmod +x post-receive

之后就可以愉快的玩耍了~