Git 跳过提交后钩子

Sre*_* TP 2 git

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

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

git commit -m "message" --no-verify
Run Code Online (Sandbox Code Playgroud)

但这并不是跳过提交后挂钩。

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

Chr*_*aes 6

文档

-n --no-verify 此选项绕过 pre-commit 和 commit-msg 钩子。另见 gitooks[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
Run Code Online (Sandbox Code Playgroud)


flu*_*ffy 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 '+---------+'
Run Code Online (Sandbox Code Playgroud)

这是 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 '+---------+'
Run Code Online (Sandbox Code Playgroud)