确保所有提交的文本文件只有 LF 换行符

Ensure that all committed text files have only LF newlines

.gitattributes 文件 * text=auto 后跟 git add --renormalize . 似乎是从存储库中清除 CRLF 的好方法。

但仍然(根据 https://git-scm.com/docs/gitattributes

When the file has been committed with CRLF, no conversion is done.

有没有办法在存储库中指定在提交时将文本文件中的所有 CRLF 转换为 LF?

是的,你可以做到。 您可以使用这样的挂钩来防止提交多个文件或用于任何其他目的,例如在您的情况下“转换”它们并验证 EOF。

pre-receive hook

#!/bin/sh

# Redirect output to screen.
exec 1>&2

# Get the list of files in the commit and "update" the file EOF as you wish
# Loop over the files and do your stuff......
for file in $( git diff-tree -r --name-only ); do
        ..... <do your stuff here > 
    done        

# Some personal message
red='3[0;31m';
green='3[0;32m';
yellow='3[0;33m';
default='3[0;m';

echo "${red}"
echo "                                         "
echo "                   |ZZzzz                "
echo "                   |                     "
echo "                   |                     "
echo "      |ZZzzz      /^\            |ZZzzz  "
echo "      |          |~~~|           |       "
echo "      |        |-     -|        / \      "
echo "     /^\       |[]+    |       |^^^|     "
echo "  |^^^^^^^|    |    +[]|       |   |     "
echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
echo "  |       |  []   /^\   []   |+[]+   |   "
echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
echo "  |[]+    |      || ||       |[]+    |   "
echo "  |_______|------------------|_______|   "
echo "                                         "
echo "                                         "
echo "  ${green}All text are fixed             " 
echo "                                         "
echo "${default}"


# set the exit code to 0 or 1 based upon your needs
# 0 = good to push
exit 0;

注:

Github不支持以这种方式使用钩子。
他们有自己的WebHooks

在这种情况下,您也可以在客户端使用挂钩。
相同的代码可以放在 客户端 .

pre-commit 挂钩中