服务器上搭建git仓库与钩子hook的配置过程
服务器上的git目录下文件配置
本服务器是基于阿里云上的服务器,服务器上的web服务在/wwwroot/www目录下
ssh root/用户名@服务器地址,进入服务器 cd /home/git/ #进入git目录 git init --bare huang.git #初始化一个git仓库


需要先在服务器上添加个Git用户组,并在git用户组里添加个git用户,后面的操作都是使用git这个用户身份操作的。因为我是root权限登录的,所以是root权限,这处必须要修改的。
chown -R git:git huang.git/


此时,git目录下的huang.git文件夹git用户组修改完毕,接下来是修改hook,此处不详述钩子的作用,这里只说明如何用,详细可以自己去查询了解。

钩子hook
进入刚创建的huang.git文件夹里的hooks文件夹中,里面有post-receive和post-update这两个文件(如果没有的话需要自己新建)
下面是配置好的post-receive里的文件内容:
#!/bin/sh # # An example hook script for the "post-receive" event. # # The "post-receive" script is run after receive-pack has accepted a pack # and the repository has been updated. It is passed arguments in through # stdin in the form # <oldrev> <newrev> <refname> # For example: # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master # # see contrib/hooks/ for a sample, or uncomment the next line and # rename the file to "post-receive". #. /usr/share/git-core/contrib/hooks/post-receive-email # cd /wwwroot/www/你的项目文件名(这里为huang) cd /wwwroot/www/huang env -i git pull
下面是配置好的post-update里的文件内容:
#!/bin/sh # # An example hook script for the "post-receive" event. # # The "post-receive" script is run after receive-pack has accepted a pack # and the repository has been updated. It is passed arguments in through # stdin in the form # <oldrev> <newrev> <refname> # For example: # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master # # see contrib/hooks/ for a sample, or uncomment the next line and # rename the file to "post-receive". #. /usr/share/git-core/contrib/hooks/post-receive-email cd /wwwroot/www/huang env -i git pull
web目录下文件配置
git目录下的文件配置完毕,接下来进入web目录/wwwroot/www。将git目录下刚创建的空仓库的内容clone过来,命令如下:

ls -la

chown -R git:git huang/

本地clone远程项目
到此,服务器上的仓库文件配置完毕。在本地clone远程服务器上的空仓库,命令如下:
git clone git@服务器地址:huang.git
进入该项目文件夹,添加代码文件
git add . git commit -m "1" git push

这个时候git目录下的代码会得到更新,同时由于钩子的作用,web目录(/wwwroot/www)下的代码也得到了同步更新。
如果web目录下的代码没有更新,可能是版本产生冲突了,可以在web目录下的项目文件夹(/wwwroot/www/huang)下git pull一下,这样代码就可以正常维护了。
注意,出现如下问题时:
remote: error: insufficient permission for adding an object to repository database ./objects
remote: fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
客户端机器对服务器git代码仓库的写权限出了问题。
改变代码仓库下,所有文件的访问权限,同组可写,确认严格按照上面的指令执行。
chown -R *
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
一文详解VSCode安装配置使用(最新版超详细保姆级含插件)
安装VScode就很简单了,一路NEXT就可以了,重点是配置使用以及插件推荐,这篇文章主要给大家介绍了关于VSCode安装配置使用的相关资料,本文是最新版超详细保姆级含插件,需要的朋友可以参考下2023-05-05


最新评论