当我尝试添加文件名在Windows上无效的文件时,我可以让git警告我吗?

时间:2014-11-28 06:39:54

标签: linux windows git

我正在Linux机器上开发。它曾经发生过,我添加了一个文件' sin(x):2.tex'或类似的东西。这对使用Windows系统且想要克隆我的存储库的朋友造成了一些麻烦。

我知道在Linux上有更多invalid file names on Windows。如果我尝试将这样一个无效的文件名添加到我的存储库中,是否有可能让git警告我?

1 个答案:

答案 0 :(得分:1)

参考pre-commit hook in this gist

,您可以看到this answer about valid Windows names的一个很好的例子
# A hook script to check that the to-be-commited files are valid
# filenames on a windows platform.
# Sources:
# - https://stackoverflow.com/a/62888
# - http://msdn.microsoft.com/en-us/library/aa365247.aspx
#
# To enable this hook, rename this file to "pre-commit", move it to ".git/hook" and make it executable

它使用git diff

git diff --cached --name-only --diff-filter=A -z $against

请注意,pre-commit挂钩是client-side hook,这意味着它必须部署在所有用户的所有存储库中。
它可以被绕过(使用git commit --no-verify)。

另一种方法是设置一个pre-receive挂钩(server-side hook),它会阻止任何推送,包括无效的文件名。