-->

specify which hook to skip on git commit --no-veri

2019-06-12 11:56发布

问题:

If there a way to specify exactly which git-hook to skip when using --no-verify? Or is there another flag other than --no-verify to accomplish this? Perhaps just a flag to only skip pre-commit?

I have two hooks that I regularly use, pre-commit and commit-msg. pre-commit runs my linting, flow check, and some unit tests. commit-msg appends my branch name to the end of the commit message.

There are times that I would like to --no-verify the pre-commit but would still like to have commit-msg run.

There seem to be a lot of SO posts similar to this, but nothing quite like selective skipping with --no-verify.

回答1:

Skipping

If you are able to modify the hooks, you can add a toggle for each one.

Example of a specific validation toggle from pre-commit.sample:

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)

So to enable toggling the entire pre-commit hook, this could be added to the beginning of it:

enabled=$(git config --bool hooks.pre-commit.enabled)

if test enabled != true
then
    echo 'Warning: Skipping pre-commit hook...'
    exit
fi

Usage example:

git -c hook.pre-commit.enabled=false commit

Aliasing

Note: Both types of aliases break tab completion.

A git alias could simplify this:

git config --global alias.noprecommit \
  '!git -c hook.pre-commit.enabled=false'

With that, you could commit and skip the hook with the following invocation:

git noprecommit commit

You could also use a shell alias (in e.g.: ~/.bashrc):

alias gitnoprecommit='git -c hook.pre-commit.enabled=false'

Usage example:

gitnoprecommit commit