git仓库同步web目录

git仓库同步web目录
 最后更新于 2024年10月02日 19:55:27

建立线上版本库

[root@VM_215_212_centos gitrepo]# git init --bare www.git
Initialized empty Git repository in /home/gitrepo/www.git/

自动同步到Web目录

自动同步功能用到的是 Git 的钩子功能

[root@VM_215_212_centos /]# cd /home/gitrepo/www.git/hooks
// 创建post-receive文件
[root@VM_215_212_centos hooks]# vim post-receive
// 在文件中写入下面内容
#!/bin/bash
git --work-tree=/home/www checkout -f

// 保存退出后,将文件所属的用户和用户组都设置成git
[root@VM_215_212_centos hooks]# chown git:git post-receive

// 设置可执行权限
[root@VM_215_212_centos hooks]# chmod +x post-receive

// 设置权限之前的文件信息
// -rw-r--r-- 1 git git   50 1月  23 09:44 post-receive

// 设置可执行权限后的文件信息
// -rwxr-xr-x 1 git git   50 1月  23 09:44 post-receive

QA

Q:Git推送成功,但是在Web目录下并没有更新文件? A:这是由于文件夹权限的原因造成的。Web目录的所属用户组为root,则必须将Git用户加入这个组,并且有操作权限