You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Git打Tag时跳过Husky的pre-push钩子?

解决pre-push钩子推送标签时自动触发的问题

直接修改你的pre-push钩子脚本,加入标签推送的判断逻辑,就能自动跳过不必要的build和commit操作,不用每次手动加--no-verify:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# 检测是否为标签推送操作
is_tag_push=false
# 读取pre-push传入的推送引用信息
while read -r local_ref local_sha remote_ref remote_sha; do
  # 远程引用以refs/tags/开头即为标签推送
  if echo "$remote_ref" | grep -q "^refs/tags/"; then
    is_tag_push=true
    break
  fi
done

# 如果是标签推送,直接退出钩子(不阻止推送)
if [ "$is_tag_push" = true ]; then
  exit 0
fi

# 非标签推送时执行原有构建逻辑
npm run build && git add dist && git commit --amend --no-edit --no-verify

逻辑说明:

  • Git的pre-push钩子会从标准输入中读取每一条要推送的引用信息,格式为本地引用 本地SHA 远程引用 远程SHA
  • 标签推送的远程引用会以refs/tags/开头,通过grep匹配这个前缀就能判断是否是标签推送
  • 一旦检测到是标签推送,直接用exit 0退出钩子,不执行后续的构建和提交操作
  • 普通分支推送(比如git push origin main)会正常执行原有逻辑

内容的提问来源于stack exchange,提问作者Vienna J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:06:20