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.
If you want to commit and bypass a single pre-commit hook use
SKIP=hook_name git commit -m "commit_message"
The SKIP environment variable is a comma separated list of hook ids. This allows you to skip a single hook instead of --no-verifying the entire commit.
SKIP=flake8,mypy git commit -m "commit_message"
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With