【发布时间】:2021-01-07 10:35:47
【问题描述】:
我的仓库中有一个 git post-commit 钩子。我有时想跳过运行这个钩子。
要跳过预提交挂钩,我知道我可以在像这样提交时使用 --no-verify 标志
git commit -m "message" --no-verify
但这并没有跳过提交后挂钩。
是否可以跳过提交后挂钩?如果有怎么办?
【问题讨论】:
标签: git
我的仓库中有一个 git post-commit 钩子。我有时想跳过运行这个钩子。
要跳过预提交挂钩,我知道我可以在像这样提交时使用 --no-verify 标志
git commit -m "message" --no-verify
但这并没有跳过提交后挂钩。
是否可以跳过提交后挂钩?如果有怎么办?
【问题讨论】:
标签: git
-n --no-verify 此选项绕过 pre-commit 和 commit-msg 挂钩。另见 githooks[5]。
所以这个标志不会跳过提交后挂钩。似乎没有一种简单、干净的方法来跳过这个标志。对于一次性操作;您可以在提交后禁用钩子并再次启用它:
chmod -x .git/hooks/post-commit # disable hook
git commit ... # create commit without the post-commit hook
chmod +x .git/hooks/post-commit # re-enable hook
【讨论】:
这是可能的。以下是我将为 Linux 和 bash 做的事情:
#!/bin/bash
# parse the command line of the parent process
# (assuming git only invokes the hook directly; are there any other scenarios possible?)
while IFS= read -r -d $'\0' ARG; do
if test "$ARG" == '--no-verify'; then
exit 0
fi
done < /proc/$PPID/cmdline
# or check for the git config key that suppresses the hook
# configs may be supplied directly before the subcommand temporarily (or set permanently in .git/config)
# so that `git -c custom.ignorePostCommitHookc=<WHATEVER HERE> commit ...` will suppress the hook
if git config --get custom.ignorePostCommitHook > /dev/null; then
exit 0
fi
# otherwise, still run the hook
echo '+---------+'
echo '| H O O K |'
echo '+---------+'
【讨论】: