git - 有什么比 .gitignore 更有效的方法来将文件保留在存储库之外?

标签 git

尽管这些文件位于 .gitignore 文件中,但我的开发团队中的人们不断将特定于构建的文件(文件夹 node_modules 和其他文件)推送到我们的存储库中,大概是 git add --all -f 或与之相关的东西。

这是一个巨大的痛苦,事实证明让人们停止这样做很困难。

有什么方法可以让我完全无法将某些文件推送到存储库吗?

最佳答案

Is there some way I can make it totally impossible to push certain files onto a repository?

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

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;

注意事项:

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

在这种情况下,您也可以使用钩子(Hook),但在客户端。
相同的代码可以放在 客户端pre-commit Hook 中。

关于git - 有什么比 .gitignore 更有效的方法来将文件保留在存储库之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400416/

相关文章:

git - 如何使用 buildbot 设置 git 提交后 Hook

java - Eclipse 上的向上箭头和数字含义

项目根目录的 Git 状态显示子模块更改,但在子模块中没有可用的

git - Hudson/Jenkins -- 如何访问 BitBucket.com 上的私有(private) git 存储库

git - 假设我是唯一处理该问题的人,则从远程存储库中删除一些提交

从 1.7.9 版本开始,git 不能在代理后面工作

git - 仅允许对明确添加的作者的更改进行审查

git - Jenkins 在从 Git 获取代码时挂起

git - 有没有办法在没有 gitignore 的情况下停止跟踪 VS Code 中的文件夹?

git - 在 git 中使用钩子(Hook)导入和导出到 csv