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

Post-checkout Hook适配Pre-Commit后未执行的问题排查求助

排查pre-commit未触发post-checkout钩子的原因

以下是逐一排查的关键点:

  • pre-commit默认不自动管理post-checkout钩子
    pre-commit核心聚焦于commit相关的钩子(如pre-commit、pre-push),要让它接管post-checkout这类git原生钩子,必须在配置中明确指定hook_type: post-checkout,同时安装时要指定钩子类型。

  • 检查.pre-commit-hooks.yaml配置
    确保钩子定义里包含hook_type字段,示例配置:

    - id: branch-warning
      name: Branch Switch Warning
      entry: ./scripts/post-checkout-warning.sh
      language: script
      hook_type: post-checkout
    
  • 检查.pre-commit-config.yaml的本地钩子引用
    如果是本地钩子,必须通过repo: local引入,并且同样指定hook_type:

    repos:
      - repo: local
        hooks:
          - id: branch-warning
            name: Branch Switch Warning
            entry: ./scripts/post-checkout-warning.sh
            language: script
            hook_type: post-checkout
    
  • 重新安装钩子并指定类型
    默认的pre-commit install只会安装commit相关钩子,需要执行:

    pre-commit install --hook-type post-checkout
    # 或者安装所有配置的钩子类型
    pre-commit install --all
    
  • 验证脚本权限
    确保你的Bash脚本有可执行权限:

    chmod +x ./scripts/post-checkout-warning.sh
    
  • 查看debug日志确认钩子触发状态
    运行checkout时开启pre-commit调试模式,查看是否有钩子调用记录:

    PRE_COMMIT_DEBUG=1 git checkout master
    

    日志中如果出现Running hook branch-warning相关内容,说明钩子已触发,问题在脚本本身;如果没有,说明配置或安装环节有问题。

  • 检查脚本参数逻辑
    git的post-checkout钩子会传入三个参数:前HEAD哈希、新HEAD哈希、分支切换标记(1为分支切换,0为文件检出)。你的脚本如果依赖这些参数判断分支切换场景,要确保参数处理逻辑正确,避免因参数不匹配导致无输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 02:45:35