Git 跳过提交后挂钩

问题描述 投票:0回答:2

我的仓库中有一个 git post-commit 挂钩。有时我想跳过运行这个钩子。

要跳过预提交挂钩,我知道我可以在像这样提交时使用 --no-verify 标志

git commit -m "message" --no-verify

但这并没有跳过提交后挂钩。

是否可以跳过提交后挂钩?如果可以怎么办?

git
2个回答
8
投票

来自文档

-n --no-verify 此选项绕过预提交和提交消息挂钩。另请参阅 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

6
投票

这是可能的。这是我对 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 '+---------+'
© www.soinside.com 2019 - 2024. All rights reserved.