[Git] Skip .git/hooks/pre-commit when committing

Tadashi Shigeoka ·  Mon, June 1, 2015

I define hooks that execute before git commit in .git/hooks/pre-commit, but sometimes don’t you want to ignore them and commit anyway?

Git

Option to skip pre-commit hook when committing

# Long option
git commit --no-verify

# Short option 
git commit -n

For example, a situation where you want to ignore pre-commit hooks and git commit would be when you’ve defined a hook that prevents commits if there’s whitespace at the end of lines, and existing code has such locations that get caught by the hook.

Here’s an example case.

This example is hard to see in the article, but var express = require(“express”); has a half-width space at the end.

$ git diff --cached

diff --git a/hoge.js b/hoge.js
new file mode 100644
index 0000000..8504c82
--- /dev/null
+++ b/hoge.js
@@ -0,0 +1 @@
+var express = require("express"); 


$ git commit

hoge.js:1: trailing whitespace.
+var express = require("express"); 

# Adding -n allows you to skip pre-hook and commit
$ git commit -n

Sometimes you want to skip pre-hooks, so remembering this should make git commit more efficient.

Reference Information

That’s all from the Gemba.