有什么比.gitignore更强大的方法来保持(强制)文件不被回购?

Tay*_*ens 19 git

我的开发团队的人员继续将构建特定文件(node_modules和其他文件)推送到我们的repos上,尽管这些文件位于.gitignore文件中,可能与之git add --all -f相关或与之相关.

这是一个巨大的痛苦,让人们停止这样做是很困难的.

有什么方法可以让某些文件完全不可能被推送到回购中吗?

Cod*_*ard 31

有什么方法可以让某些文件完全不可能被推送到回购中吗?

是的,您可以使用这样的钩子来防止提交多个文件.

pre-receive hook

#!/bin/sh

# Check to see if this is the first commit in the repository or not
if git rev-parse --verify HEAD >/dev/null 2>&1
then
    # We compare our changes against the previous commit
    against=HEAD^
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to screen.
exec 1>&2

# Check to see if we have updated the given file
if [ $(git diff-tree -r --name-only $against | grep <ANY FILE YOU WANT TO FIND OUT HERE> ) ];
then

    # Output colors
    red='\033[0;31m';
    green='\033[0;32m';
    yellow='\033[0;33m';
    default='\033[0;m';

    # personal touch :-)
    echo "${red}"
    echo "                                         "
    echo "                   |ZZzzz                "
    echo "                   |                     "
    echo "                   |                     "
    echo "      |ZZzzz      /^\            |ZZzzz  "
    echo "      |          |~~~|           |       "
    echo "      |        |-     -|        / \      "
    echo "     /^\       |[]+    |       |^^^|     "
    echo "  |^^^^^^^|    |    +[]|       |   |     "
    echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
    echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
    echo "  |       |  []   /^\   []   |+[]+   |   "
    echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
    echo "  |[]+    |      || ||       |[]+    |   "
    echo "  |_______|------------------|_______|   "
    echo "                                         "
    echo "                                         "
    echo "      ${green}You have just committed code  " 
    echo "      ${red}Your code ${yellow}is bad.!!!      "
    echo "      ${red} Do not ever commit again    "
    echo "                                         "
    echo "${default}"
fi;

# set the exit code to 0 or 1 based upon your needs
# 0 = good to push
# 1 = exit without pushing.
exit 0;
Run Code Online (Sandbox Code Playgroud)

注意:

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

在这种情况下,您也可以使用挂钩,但在客户端.
相同的代码可以放在客户端的pre-commit钩子里面.

  • 客户端钩子的问题是你可以删除它们(用户).在服务器端,只有git admin可以更改它们 (2认同)