Git 由于 sourcetree 上的 husky 预推导致推送失败

Git push failed due to husky pre-push on sourcetree

在推送 React Native 项目时,由于 husky 预推送失败而出现错误

husky > pre-push hook failed (add --no-verify to bypass)

显示的所有这些错误都是 lint 错误,如下所示

unused-vars

27:48  error    Trailing spaces not allowed    
                     no-trailing-spaces

75:5   warning  Unexpected console statement   
                     no-console

92:93  error    Unexpected trailing comma   
                        comma-dangle

96:81  error    Unexpected trailing comma

如何在 mac 上的 Sourcetree 应用程序中关闭此功能?

这个问题(即使它不是真正的问题!)是因为 React 创建的钩子。解决方案是简单地删除 git 的 hooks 文件夹,它定义了预提交挂钩,因此可以在此之后推送。 (这只是一种避免因 lint 错误而一次编辑数千个文件的 hack。遵循指南并解决所有 lint 错误以获得更好的代码质量和可维护性。)

编辑:您也可以在提供 git 命令行参数时跳过挂钩 —no-verify, git push origin master --no-verify,或使用 Sourcetree 的绕过提交挂钩设置(在提交消息字段右上角的菜单中)

将 --no-verify 添加到短期提交的末尾。

我也在 Mac 并开始看到这些我认为是在我在主项目文件夹中实例化的 Carlo 应用程序上工作。由于我不确定 Husky 是什么('husky' 命令未安装),我通过谷歌搜索堆栈溢出,所以我开始四处寻找 linter,猜测是尝试 eslint 。

➜  src_aminosee git:(master) ✗ eslint .
Error: Cannot find module '@ljharb/eslint-config'
Referenced from: /Users/tom/Dropbox/Sites/funk.co.nz/aminosee/carlojet/node_modules/array-includes/.eslintrc

这时我才意识到我在 git 存储库中有一个 git 存储库('carlojet' 在 'aminosee' 主项目中尝试文件夹)!我必须(应该)将该文件夹移出。不确定这种类型的嵌套回购问题是否是导致您的问题的原因,但是在如此移动并尝试提交之后我看到:

git commit -am "moved carlojet folder out as i think its git repo conflicted with this main one"
Can't find Husky, skipping pre-commit hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook
Can't find Husky, skipping prepare-commit-msg hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook
Can't find Husky, skipping commit-msg hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook
Can't find Husky, skipping post-commit hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook

我对 git 的了解不足,但对我来说,这些文件现在正在挥手 "good bye" 离开了仓库;或者更可能的是 "winking" 我从他们在 aminosee/carlojet/ 的真正家进入了他们的 parents aminosee/.git/hooks/ 目录(这真是一个惊喜!)。 git/hooks/

我可能需要禁用从其他项目复制的所有这些挂钩...或者最好还是调出那个 linter!我想在我的例子中嵌套 git repo 不是一个好主意。

我认为帮助您理解 husky 工具同样重要。
我发现这 article 对处理这种情况非常有帮助,当我也很挣扎时。

Husky is an npm package that lets you define npm scripts that correlate to local Git events such as a commit or push. And this helps in enforcing collaborative standards in a project.

在您的项目中,您提到所有错误都与 linting 相关。
所以在那里,husky 脚本被编写来创建一个 git hook,称为 pre-push,它在你可以 git push 之前强制执行 code linting成功。

在我看来,特别是如果您在团队中工作,请不要 turn-off/deactivate 这些检查,也不要删除 .git/hooks 文件夹。而是返回并 运行 lint script(通常在 package.json 中找到),修改所需的更改,一旦你再次 git push 你就会成功。

--no-verify 标记添加到推送命令的末尾。

git push origin master --no-verify