696
votes

I'm looking at a git hook which looks for print statements in Python code. If a print statement is found, it prevents the git commit.

I want to override this hook and I was told that there is a command to do so. I haven't been able to find it. Any thoughts?

5
A solution that is more generic (also work for git commands different than commit): stackoverflow.com/questions/58337861/…Gabriel Devillers

5 Answers

1182
votes

Maybe (from git commit man page):

git commit --no-verify

-n  
--no-verify

This option bypasses the pre-commit and commit-msg hooks. See also githooks(5).

As commented by Blaise, -n can have a different role for certain commands.
For instance, git push -n is actually a dry-run push.
Only git push --no-verify would skip the hook.


Note: Git 2.14.x/2.15 improves the --no-verify behavior:

See commit 680ee55 (14 Aug 2017) by Kevin Willford (``).
(Merged by Junio C Hamano -- gitster -- in commit c3e034f, 23 Aug 2017)

commit: skip discarding the index if there is no pre-commit hook

"git commit" used to discard the index and re-read from the filesystem just in case the pre-commit hook has updated it in the middle; this has been optimized out when we know we do not run the pre-commit hook.


Davi Lima points out in the comments the git cherry-pick does not support --no-verify.
So if a cherry-pick triggers a pre-commit hook, you might, as in this blog post, have to comment/disable somehow that hook in order for your git cherry-pick to proceed.
The same process would be necessary in case of a git rebase --continue, after a merge conflict resolution.

28
votes

From man githooks:

pre-commit
This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with non-zero status from this script causes the git commit to abort.

21
votes

For those very beginners who has spend few hours for this commit (with comment and no verify) with no further issue

git commit -m "Some comments" --no-verify
3
votes

--not-verify works, but in my case, I didn't want to put the parameter all the time on the terminal. So I opted for something a little more aggressive.

If you want to disable git hooks globally, you could try running this:

git config --global core.hooksPath /dev/null

But, if you want to leave it as it was before, just run the following command in your terminal:

git config --global --unset core.hooksPath

If you do not want it to be global just remove the argument: --global

I hope it will be useful to someone, I tested it with git 2.16.3

0
votes

for some reason, --no-verify does not work for me with this particular hook: prepare-commit-msg

if you too are running into this issue, try:

SKIP=prepare-commit-msg git commit