我在我的代码库中有一个git提交后钩子。有时候我想跳过运行此钩子。
要跳过pre-commit钩子,我知道可以在提交时使用--no-verify标志,就像这样:
git commit -m "message" --no-verify
但这并不是跳过提交后钩子。
是否有可能跳过提交后钩子?如果可以,如何操作?
我在我的代码库中有一个git提交后钩子。有时候我想跳过运行此钩子。
要跳过pre-commit钩子,我知道可以在提交时使用--no-verify标志,就像这样:
git commit -m "message" --no-verify
但这并不是跳过提交后钩子。
是否有可能跳过提交后钩子?如果可以,如何操作?
根据文档:
-n --no-verify 该选项跳过 pre-commit 和 commit-msg 钩子。参见 githooks[5]。
因此使用该标记不会跳过 post-commit 钩子,似乎没有一种简单、干净的方法来跳过该标记。对于一次性操作,您可以禁用钩子,并在提交后再次启用它:
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 '+---------+'
这里是Mac的变体版本。
#!/bin/bash
# parse the command line of the parent process
# (assuming git only invokes the hook directly; are there any other scenarios possible?)
args=$(ps -o args -p $PPID)
if echo "$args" | grep --quiet -- '--no-verify'; then
exit 0
fi
# 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 '+---------+'