当前位置: 代码网 > it编程>编程语言>其他编程 > 服务器上搭建git仓库与钩子hook的配置过程

服务器上搭建git仓库与钩子hook的配置过程

2026年05月07日 其他编程 我要评论
服务器上的git目录下文件配置本服务器是基于阿里云上的服务器,服务器上的web服务在/wwwroot/www目录下ssh root/用户名@服务器地址,进入服务器cd /home/git/ #进入g

服务器上的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 *

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com