什么是 Git Hooks?

发布于:2025-02-11 ⋅ 阅读:(69) ⋅ 点赞:(0)

在团队开发中,当成员提交代码的描述信息不符合约定提交规范的时候,需要阻止当前的提交,而要实现这个目的,我们就需要先来了解一个概念,叫做 Git hooks,即Git 在执行某个事件之前或之后进行一些其他额外的操作。

而我们所期望的 阻止不合规的提交消息,那么就需要使用到 hooks 的钩子函数。

相关的 hooks 非常多,我们用的比较多的其实只有两个。

1. commit-msg:可以用来规范化标准格式,并且可以按需指定是否要拒绝本次提交;

2. pre-commit:会在提交前被调用,并且可以按需指定是否要拒绝本次提交;

下面是我整理出来的所有的 hooks ,大家可以进行一下参考,其中红色的是常用到的 hooks。

PS:详细的 hooks 介绍 可点击这里查看 

Git Hook 调用时机 说明
pre-applypatch git am执行前
applypatch-msg git am执行前
post-applypatch git am执行后 不影响git am的结果
pre-commit git commit执行前 可以用git commit --no-verify绕过
commit-msg git commit执行前 可以用git commit --no-verify绕过
post-commit git commit执行后 不影响git commit的结果
pre-merge-commit git merge执行前 可以用git merge --no-verify绕过。
prepare-commit-msg git commit执行后,编辑器打开之前 
pre-rebase git rebase执行前
post-checkout git checkout或git switch执行后 如果不使用--no-checkout参数,则在git clone之后也会执行。
post-merge git commit执行后 在执行git pull时也会被调用
pre-push git push执行前
pre-receive git-receive-pack执行前
update    
post-receive git-receive-pack执行后 不影响git-receive-pack的结果
post-update 当 git-receive-pack对 git push 作出反应并更新仓库中的引用时    
push-to-checkout 当git-receive-pack对git push做出反应并更新仓库中的引用时,以及当推送试图更新当前被签出的分支且receive.denyCurrentBranch配置被设置为updateInstead时
pre-auto-gc git gc --auto执行前    
post-rewrite 执行git commit --amend或git rebase时
sendemail-validate git send-email执行前    
fsmonitor-watchman 配置core.fsmonitor被设置为.git/hooks/fsmonitor-watchman或.git/hooks/fsmonitor-watchmanv2时
p4-pre-submit git-p4 submit执行前    可以用git-p4 submit --no-verify绕过
p4-prepare-changelist git-p4 submit执行后,编辑器启动前 可以用git-p4 submit --no-verify绕过
p4-changelist git-p4 submit执行并编辑完changelist message后 可以用git-p4 submit --no-verify绕过
p4-post-changelist git-p4 submit执行后    
post-index-change 索引被写入到read-cache.c do_write_locked_index后

上面介绍了Git hooks的基本概念,下一篇文章中将会向大家介绍《使用 commitlint 和 husky 检查提交描述是否符合规范要求》,该篇将会讲述如何实现阻止不符合要求的代码描述提交。


网站公告

今日签到

点亮在社区的每一天
去签到