git hooks 入门及实例

本文介绍了git hooks的基本概念,并通过实例展示了如何在commit前使用coffeelint确保代码质量,以及服务端的post-receive钩子用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[要敢于接受一些挑战,那才是快速学习的机会。]



1.定义

  git的挂钩即位于./git/hooks目录下的一些脚本


2.分类

   (1)服务器挂钩,用于 Git 服务器端的操作,如接收被推送的提交
   (2)客户端挂钩,用于客户端的操作,如提交和合并


3.常用挂钩

(1)pre-commit(客户端):键入提交信息时运行,主要用于检查是否有东西遗漏,测试是否正确,代码规范检查,可以用git commit --no-verify来忽略
(2)post-receive(服务端):客户端向服务器push后,服务器接收后的自动运行脚本,可以实现master和develop分支的自动更新


4.examples
(1)pre-commit(客户端):
  简单的安装就是直接cp .git/hooks/pre-commit.sample .git/hooks/pre-commit 
  然后修改文件即可
  但是考虑到以后的迁移,最好使用如下命令:
  touch /path/to/repo/.git/hooks/pre-commit
  chmod a+x /path/to/repo/.git/hooks/pre-commit

  code如下:

# test for coffeelint 
test=`coffeelint ./ | grep Ok! | awk '{ print $1 }'`
if [ "test" != '✓']; then
 coffeelint ./
 echo "Coffeelint test failed."
 exit 1
else
 echo "Coffeelint test passed."
fi

上述为在commit之前对所有文件做coffeelint,全部通过才可以commit,否则返回1,commit失败


(2)post-receive(服务端):

code如下:

cd path/to/service/repo
env -i git checkout master
env -i git pull origin master
env -i git checkout develop
env -i git pull origin develop

PS:注意env -i不可或缺

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值