Git(当前)没有可以用来做你想做的事情的钩子。有关git help hooks可用挂钩的列表,请参阅。
您可能需要考虑不同的方法。例如,您可以包装git一个包装脚本或 shell 函数,它们自己进行解析以防止您删除分支:
git() {
[ "${1}" != branch ] ||
{ [ "$2" != -d ] && [ "$2" != -D ]; } ||
case $3 in
abranchthatshouldnotbedeleted) false;;
*) true;;
esac ||
{ printf %s\\n "ERROR: branch $3 must not be deleted" >&2; exit 1; }
command git "$@"
}
上面的 shell 函数非常原始,不处理类似的调用git --git-dir=/foo.git branch -d abranchthatshouldnotbedeleted,但你明白了。也许您可以使用git rev-parse --parseopt它来使其更健壮。