如何防止 git commit --no-verify 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40519739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:25:16  来源:igfitidea点击:

How to prevent git commit --no-verify command?

gitgithubgitlab

提问by albert

I would like to setup a pre-commit hook for all git repos to validate syntax errors using jshint and phplint. But the issue is that the git has a feature which can skip pre-commit hook from happening by using --no-verify flag. But i don't need to use that option. Can i prevent that --no-verify flag for git Please suggest a way.

我想为所有 git repos 设置一个预提交钩子,以使用 jshint 和 phplint 验证语法错误。但问题是 git 有一个功能,可以通过使用 --no-verify 标志跳过预提交钩子的发生。但我不需要使用那个选项。我可以阻止 git 的 --no-verify 标志吗?请提出一种方法。

回答by candu

First off: you can't definitively prevent someone from passing the --no-verifyoption. That said, it's a good practice to use pre-commithooks for linting, and it's a good practice to avoid passing the --no-verifyoption without reason.

首先:您无法明确阻止某人通过该--no-verify选项。也就是说,使用pre-commit钩子进行 linting 是一种很好的做法,并且避免--no-verify无故传递选项是一种很好的做法。

If, however, you want to make it more cumbersome to pass the --no-verifyoption, you could:

但是,如果您想让传递--no-verify选项变得更麻烦,您可以:

  1. generate a verification token and append it to the commit message in pre-commit;
  2. exit pre-receivewith a non-zero exitcode if this token is missing or invalid. (Examples of things you can do in pre-receive hooks: https://github.com/github/platform-samples/tree/master/pre-receive-hooks)
  1. 生成验证令牌并将其附加到提交消息中pre-commit
  2. pre-receive如果此令牌丢失或无效,则以非零退出代码退出。(你可以在预接收钩子中做的事情的例子:https: //github.com/github/platform-samples/tree/master/pre-receive-hooks

Someone determined to avoid passing --no-verifycould manually do step 1, which is why this isn't 100% effective. I wouldn't recommend setting this up in a professional context, but I'm all for people using the tools at their disposal to instill good habits for themselves, while learning more about git hooks.

决定避免通过的人--no-verify可以手动执行第 1 步,这就是为什么这不是 100% 有效的原因。我不建议在专业环境中设置它,但我完全支持人们使用他们可以使用的工具为自己灌输良好的习惯,同时更多地了解 git hooks。