添加新的 git hook 后“Hook 已存在:预推送”

问题描述 投票:0回答:2

在 git 存储库中,我们已经有了 .Net 解决方案,最近我们添加了一个角度部分。

对于这个有角度的部分,我们确实添加了一些哈士奇的钩子。由于包含 package.json 的前端位于子文件夹中,因此我们执行了以下操作:

  1. npm 安装 husky --save-dev
  2. cd .. && husky install ./Frontend/.husky
  3. 添加了“准备”脚本:
    "prepare": "cd .. && husky install ./Frontend/.husky"
  4. npx husky add .husky/pre-commit "cd ./Frontend && npm run lint"
  5. 我们还添加了一个钩子来在获取代码后进行自动 npm 安装:
    npx husky add .husky/post-merge "cd ./Frontend && npx git-pull-run --pattern 'package-lock.json' --command 'npm install'"

这似乎在本地工作正常,但是当我们的 CI 代理(azure devop)检出存储库时,我们收到错误:

Syncing repository: XXX (Git)
Prepending Path environment variable with directory containing 'git.exe'.
git version
git version 2.30.2.windows.1
git lfs version
git-lfs/2.13.3 (GitHub; windows amd64; go 1.16.2; git a5e65851)
git config --get remote.origin.url
git clean -ffdx
git reset --hard HEAD
git config gc.auto 0
git config --get-all http.https://[email protected]/yyy/zzz/_git/aaa.extraheader
git config --get-all http.extraheader
git config --get-regexp .*extraheader
git config --get-all http.proxy
git config http.version HTTP/1.1
git lfs install --local
Hook already exists: post-merge

    #!/usr/bin/env sh
    . "$(dirname -- "$0")/_/husky.sh"
    
    cd ./Frontend && npx git-pull-run --pattern 'package-lock.json' --command 'npm install'

To resolve this, either:
  1: run `git lfs update --manual` for instructions on how to merge hooks.
  2: run `git lfs update --force` to overwrite your hook.
##[error]Git-lfs installation failed with exit code: 2

当 azure Devop 具有干净的存储库时,不会发生此错误,但仅在第二次运行时发生。

我尝试运行 git lfs update --manual/--force 命令,不进行任何更改。

如何正确集成 git-lfs 和 git hooks?

git git-lfs husky
2个回答
1
投票

Husky 和 LFS 使用一些相同的钩子,您在此处看到的错误消息是由于 LFS 未能安装 Husky 已经“保留”的钩子所致。

这里记录了一个很棒的解决方案: https://dev.to/mbelsky/pair-husky-with-git-lfs-in-your-javascript-project-2kh0

简而言之,您需要先安装 LFS hooks,将它们移动到单独的目录,然后安装 Husky hooks。

$ rm -rf .git/hooks
$ git lfs install
$ mv .git/hooks ./lfs-hooks
$ rm -rf node_modules/husky
$ npm install

然后您需要更新 Husky 配置,以确保 LFS 在您需要时仍然运行(因为它不再在 .git/hooks 文件夹中具有钩子)。

  "husky": {
    "hooks": {
      "post-checkout": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-checkout $HUSKY_GIT_PARAMS",
      "post-commit": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-commit $HUSKY_GIT_PARAMS",
      "post-merge": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-merge $HUSKY_GIT_PARAMS",
      "pre-push": "echo $HUSKY_GIT_STDIN | lfs-hooks/pre-push $HUSKY_GIT_PARAMS"
    }
  },

或者,git-hooks 显然为那些乐于尝试其他工具的人提供了与 LFS 更好的集成。

请注意,此问题仅影响 Husky 的某些早期版本,因此听起来您可能在本地使用较新版本,但在管道中使用较旧版本。


0
投票

我只是寻找解决方案并降落在这里。检查参考文章等

在当前版本的

git lfs install
中,我得到以下输出:

╰─ git lfs install
Hook already exists: pre-push

        #!/usr/bin/env sh
        . "$(dirname "$0")/h"

To resolve this, either:
  1: run `git lfs update --manual` for instructions on how to merge hooks.
  2: run `git lfs update --force` to overwrite your hook.

有使用提示

git lfs update --force
,说明书如下:

╰─ git lfs update --manual
Add the following to '.husky/_/pre-push':

        #!/bin/sh
        command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
        git lfs pre-push "$@"

Add the following to '.husky/_/post-checkout':

        #!/bin/sh
        command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
        git lfs post-checkout "$@"

Add the following to '.husky/_/post-commit':

        #!/bin/sh
        command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
        git lfs post-commit "$@"

Add the following to '.husky/_/post-merge':

        #!/bin/sh
        command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
        git lfs post-merge "$@"

对我来说,这似乎是一个更可行的解决方案,因为每个 git 项目在这种情况下都有些特殊。

© www.soinside.com 2019 - 2024. All rights reserved.