添加新的 git 钩子后"Hook already exists: pre-push"



在我们已经有。net解决方案的git存储库中,我们最近添加了一个angular部分。

对于这个有棱角的部分,我们确实用husky添加了一些钩子。因为前端,所以包含了包。Json在子文件夹中,我们做了以下操作:

  1. npm install husky——save-dev
  2. Rancd .. && husky install ./Frontend/.husky
  3. 添加了"prepare"脚本:"prepare": "cd .. && husky install ./Frontend/.husky"
  4. rannpx 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://xxx@dev.azure.com/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开发有一个干净的存储库时,这个错误不会发生,但只有在第二次运行时才会发生。

我尝试运行git lfs update——manual/——force命令,没有任何更改。

如何正确集成git-lfs与git钩子?

Husky和LFS使用一些相同的钩子,你在这里看到的错误消息是由于LFS未能安装Husky已经"保留"的钩子。

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

简而言之,你需要先安装LFS钩子,将它们移动到一个单独的目录,然后安装Husky钩子。

$ 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,因此听起来您可能在本地使用较新版本,但管道中使用较旧版本。

最新更新