简体   繁体   English

Git设置跳过某些文件的预提交挂钩

[英]Git setting to skip a pre-commit hook for certain files


Disclaimer: yes, I've read Skip Git commit hooks and I know about the -n flag, but this is not what I'm looking for. 免责声明:是的,我已经阅读了Skip Git提交钩子 ,我知道-n标志,但这不是我想要的。


I have that pre-commit hook that runs in order to do some code prettifying (replace tabs with spaces, remove double empty lines, ...). 我有预先提交的钩子运行,以便做一些代码美化(用空格替换制表符,删除双空行,...)。

However, there are some use cases where I don't want the hook to run: for example, when I'm checking in some Makefiles, I cannot simply replace tabs with spaces, so I'd have to skip these files. 但是,有一些用例我不希望钩子运行:例如,当我检查一些Makefile时,我不能简单地用空格替换制表符,所以我必须跳过这些文件。

If I were the only developer, I would be fine with the -n flag in order to prevent the hook from being run, but there are other guys that use UIs that don't know the -n flag ( cough tortoisegit) and they won't be willing to specify some flags to commit . 如果我是唯一的开发人员,我会很好地使用-n标志以防止钩子被运行,但是还有其他人使用不知道-n标志的UI( 咳嗽 tortoisegit)他们赢了不愿意指定一些标志来commit

Apart of implementing the skipping directly in the hook and the obvious -n way: are there any other ways how I could prevent the pre-commit hook to be run? 除了直接在钩子中实现跳过和明显的-n方式之外:还有其他方法可以阻止预提交挂钩运行吗?

Following a similar idea as this pre-commit hook , you could use: 遵循与此预提交挂钩类似的想法,您可以使用:

git diff --cached --name-only

That would list the files that are about to be committed, and you can then filter on them in order to apply your controls or to skip those files. 这将列出即将提交的文件,然后您可以对它们进行过滤以应用控件或跳过这些文件。

In other words, the hook is still running on every commit, for every files. 换句话说,对于每个文件,钩子仍然在每次提交时运行。
But its script is smart enough to apply its test to the right files within the list of files to be committed. 但它的脚本非常智能,可以将其测试应用于要提交的文件列表中的正确文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM