6

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.

Jeremy
  • 1,717
  • 5
  • 30
  • 49

2 Answers2

7

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
kelvin
  • 1,421
  • 13
  • 28
  • E.g. `git -c hooks.pre-commit.enabled=false cherry-pick --continue`.You need to have added the default to git config (as a one-off), like so: `git config hooks.pre-commit.enabled true` – avjaarsveld Oct 02 '19 at 09:43
7

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"

Abilash B
  • 71
  • 1
  • 2
  • It might be unclear but this applies to `pre-commit` tool (https://pre-commit.com/) that manages your hook installations. Doesn't apply to `git` itself. – smido Jul 07 '23 at 09:47