尽管这些文件位于 .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/