1119

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?

2

7 Answers 7

1825

Maybe (from git commit man page):

git commit --no-verify -m "commit message"
           ^^^^^^^^^^^
-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.


With Git 2.36 (Q2 2022), the callers of run_commit_hook() to learn if it got "success" because the hook succeeded or because there wasn't any hook.

See commit a8cc594 (fixed with commit 4369e3a1), commit 9f6e63b (07 Mar 2022) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 7431379, 16 Mar 2022)

hooks: fix an obscure TOCTOU "did we just run a hook?" race

Signed-off-by: Ævar Arnfjörð Bjarmason

Fix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee55 ("commit: skip discarding the index if there is no pre-commit hook", 2017-08-14, Git v2.15.0-rc0 -- merge listed in batch #3).

This obscure race condition can occur if we e.g. ran the "pre-commit" hook and it modified the index, but hook_exists() returns false later on (e.g., because the hook itself went away, the directory became unreadable, etc.).
Then we won't call discard_cache() when we should have.

The race condition itself probably doesn't matter, and users would have been unlikely to run into it in practice.
This problem has been noted on-list when 680ee55 was discussed, but had not been fixed.

Let's also change this for the push-to-checkout hook.
Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout.
If the hook doesn't exist we'll fall back on push to deploy.
The same behavior as before, without the TOCTOU race.
See 0855331 ("receive-pack: support push-to-checkout hook", 2014-12-01, Git v2.4.0-rc0 -- merge) for the introduction of the previous behavior.

This leaves uses of hook_exists() in two places that matter.
The "reference-transaction" check in refs.c, see 6754159 ("refs: implement reference transaction hook", 2020-06-19, Git v2.28.0-rc0 -- merge listed in batch #7), and the "prepare-commit-msg" hook, see 66618a5 ("sequencer: run 'prepare-commit-msg' hook", 2018-01-24, Git v2.17.0-rc0 -- merge listed in batch #2).

In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook.
So using this "invoked_hook" pattern doesn't make sense in those cases.

The "reference-transaction" and "prepare-commit-msg" hook also aren't racy.
In those cases we'll skip the hook runs if we race with a new hook being added, whereas in the TOCTOU races being fixed here we were incorrectly skipping the required post-hook logic.

9
  • 2
    For cherry-pick see web-dev.wirt.us/info/git-drupal/git-continue-vs-no-verify
    – Davi Lima
    Commented Sep 1, 2019 at 12:31
  • 1
    @DaviLima Thank you. I have included your comment in the answer for visibility.
    – VonC
    Commented Sep 1, 2019 at 13:39
  • 1
    thanks, @Vonc. same happens for git rebase --continue btw
    – Davi Lima
    Commented Sep 2, 2019 at 14:26
  • Hi, in my case, someone in my team keep updating my hook folder with some pre-commit scripts, it is so annoying, is there a way I can prevent my hook folder to be overriden by git pull? Commented May 21, 2020 at 10:49
  • 1
    yep. Had to rename .git\hooks\pre-commit and rename it back afterwards. I'm surprised there isn't a flag to pass flags to commit Commented Jun 9, 2021 at 20:51
85

With both comment and no verify without any further issue:

git commit -m "Some comments" --no-verify
4
  • 2
    @Moumit The command just works fine. You might want to rephrase the sentence as this is little confusing. Commented Nov 29, 2021 at 9:09
  • Understood the concern changed it @PramodItagi
    – Moumit
    Commented Nov 29, 2021 at 10:59
  • 1
    What do you mean by "with no further issue"? Can you elaborate? And what do you mean by "with both comment and no verify"? It would be fine to elaborate here in comments. And/or by editing your answer. Commented Dec 2, 2021 at 15:05
  • 1
    "-m" is "Use the given <msg> as the commit message.". By comment, do you mean the (commit) message? Commented Jul 5, 2022 at 20:23
59

--no-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 tested it with Git 2.16.3.

1
  • 4
    Thanks Francisco! Works like a charm! I ended up adding the following aliases to my $HOME/.gitconfig -> disable-hooks = config core.hooksPath /dev/null and enable-hooks = config --unset core.hooksPath.
    – jweyrich
    Commented May 3, 2022 at 18:41
34

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.

0
31

-n or --no-verify does not work for a commit after 'git merge --continue' for example.

So here is another rougher idea.

  1. Just comment lines in file .git/hooks/pre-commit with symbol '#'.
  2. Run single or many commands
  3. Uncomment
  4. Profit.
17

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
3
  • 1
    This didn't work for me. Is the SKIP env var documented by git? Or is this some special control flow implemented by your prepare-commit-msg script?
    – Jasha
    Commented Jun 11, 2022 at 21:32
  • 3
    @Jasha: it's not a Git standard thing.
    – torek
    Commented Jun 15, 2022 at 11:17
  • I think this SKIP is part of the pre-commit tool and not part of core Git. See: pre-commit.com/index.html#temporarily-disabling-hooks Commented May 9, 2023 at 10:58
-5

SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^(.>).$/- signed-off-by: \1/p') grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

1
  • This “signed-off-by” stuff is irrelevant. Did you reply to the wrong answer? Although it’s been half a week since I first saw this answer so you should have noticed by now. Commented May 12, 2023 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.