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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值